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 6577325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:37:16+00:00 2026-05-25T15:37:16+00:00

I have scope scope :for_list, lambda { |brand_ids| where(‘brands.id IN (?)’, brand_ids).includes(:models).where(models.popular = ‘1’).order(‘models.name’)

  • 0

I have scope

scope :for_list, lambda { |brand_ids|
   where('brands.id IN (?)', brand_ids).includes(:models).where("models.popular = '1'").order('models.name')
}

But some times there are no models.popular = 1

And in this case I want to select all from models ignoring popular parameter

How to write that 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-05-25T15:37:17+00:00Added an answer on May 25, 2026 at 3:37 pm

    Since it’s simply a lambda function, you can use an if/else in there and it’ll get evaluated at the time it’s called. Therefore, you could write the scope like this:

    scope :for_list, lambda { |brand_ids|
        if self.popular > 1
               where('brands.id IN (?)', brand_ids).includes(:models).where("models.popular = '1'").order('models.name')
        else
               where('brands.id IN (?)', brand_ids).includes(:models).order('models.name')
        end
    }
    

    That should work. However, I’d caution against using this. That logic doesn’t belong in a scope. Instead, you should question you your views and controllers are set up and rework those.

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

Sidebar

Related Questions

So I have some settings that are of the user scope, but for some
The scope of this is that we have three main projects. Some of the
I have the following models but I cannot get the custom method reorder_action_items to
I have this printf statement: printf(name: %s\t args: %s\t value %d\t arraysize %d\t scope
I have a List of MyBean1 in request scope (they have the name tests
I have a number of application settings (in user scope) for my custom grid
When I create my feature I have the following choices for feature scope: WEB
What is the scope of variables in javascript? Do they have the same scope
I have the following named_scope which works fine in MySQL and sqlite but bombs
I have a BizTalk (2006 R2) scope with a Catch Exception part in which

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.