我有四個型號在我的應用程序,定義爲named_scope正確的語法如下的Rails 3:與方法調用和模型關聯
class User < ActiveRecord::Base
has_many :comments
has_many :geographies
has_many :communities, through: :geographies
class Comment < ActiveRecord::Base
belongs_to :user
class Community < ActiveRecord::Base
has_many :geographies
has_many :users
class Geography < ActiveRecord::Base
belongs_to :user
belongs_to :community
用戶可以發表評論,這是與一個或多個社區通過地理相關表。
我的任務是僅顯示從下拉列表中選擇的社區評論。我從this post得知我可以通過comment.user.communities.first
對象鏈訪問給定評論的社區。
這似乎通常與拉姆達一個named_scope將過濾的所有評論列表中的首選,但是,我是在一個完全喪失瞭如何構建這個named_scope。我試圖通過遵循一些RailsCasts來構造named_scope,但是就我所能得到的而言,這已經是了。生成的錯誤如下。
class Comment < ActiveRecord::Base
belongs_to :user
def self.community_search(community_id)
if community_id
c = user.communities.first
where('c.id = ?', community_id)
else
scoped
end
end
named_scope :from_community, { |*args| { community_search(args.first) } }
這是錯誤:
syntax error, unexpected '}', expecting tASSOC
named_scope :from_community, lambda { |*args| { community_search(args.first) } }
^
什麼是用於傳遞方法與參數到一個named_scope正確的語法?