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?
Since it’s simply a lambda function, you can use an
if/elsein there and it’ll get evaluated at the time it’s called. Therefore, you could write the scope like this: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.