我有四个型号在我的应用程序,定义为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正确的语法?