Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:50:38+00:00 2026-06-07T08:50:38+00:00

I have four models in my application, defined as follows class User < ActiveRecord::Base

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:50:40+00:00Added an answer on June 7, 2026 at 8:50 am

    First, you can just use scope now in rails 3 – the older named_scope form was shortened, and it’s removed in rails 3.1!

    With regard to your error, though, I suspect you don’t need the inner set of brackets. They are usually doubled when using a lambda block like that, because you are creating new hash from scratch, like this:

    scope :foo, { |bar|
      { :key => "was passed #{bar}" }
    }
    

    In your case, though, your are calling community_search which should be returning a value that you can return directly. In this case, an AREL object that has replaced such simple hashes. It’s somewhat confusing when reading all the random posts and tutorials out there on this subject, largely due to the huge change in style that AREL caused. Both of those style use use are ok, though – either as a lambda or class method. They largely mean the same thing. The two above links have several examples of this newer style for further reading.

    Of course, you could just learn something like squeel, which I find much easier to read, and cuts out a lot of the typing. ^^;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many
I have four models: User Award Badge GameWeek The associations are as follows: User
I have Four models. Product Category Categorization Images Product.rb has_many :images has_many :categorizations has_many
I have four models in my models.py which are: models.py class Course(models.Model): course_code =
i am having a four models.. User,post,tag,comment.. the relationship i want to have with
I have four models that are related to one another, the way I have
i have four tables user-question contains two columns: questionID, userID, the questions that the
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
I have on master page four user controls which are binded to shared presenter.
I'm using Mongoid, Devise and Rails 3.1. I have four models: Students, Teacher, Parents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.