I have four models in my application, defined as follows
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
Users can post comments, which are associated with one or more communities through the geography table.
My task is the display only comments from the community selected from a dropdown list. I learned from this post that I can access the community of a given comment via the comment.user.communities.first
object chain.
It seems typically a named_scope with lambda would be the preferred choice to filter the list of all comments, however, I am at a complete loss how to construct this named_scope. I've tried to construct the named_scope by following some RailsCasts, but this is as far as I was able to get. The generated error is below.
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) } }
This is the error:
syntax error, unexpected '}', expecting tASSOC
named_scope :from_community, lambda { |*args| { community_search(args.first) } }
^
What is the correct syntax for passing a method with arguments into a named_scope?