Can I create a scope named * un * in Rails?

I know that you can create named scopes in Rails that allow you to specify conditions that can then be built later:

named_scope :active, :conditions => {:active => true} ... MyModel.active.find(...) 

This works by creating a proxy object that does not take longer to evaluate. I want to know if it is possible to create a dynamic nameless region?

By this I mean, is there a foo method that I can go with

 scope = MyModel.foo(:conditions => {:target_id => 4}) 

and then pass scope around as a proxy object, which can I make more .find or other scope calls?

+4
source share
1 answer

Yes, check Anonymous Areas :

 def find_products scope = Product.scoped({}) scope = scope.conditions "products.name LIKE ?", "%#{keywords}%" unless keywords.blank? scope = scope.conditions "products.price >= ?", minimum_price unless minimum_price.blank? scope = scope.conditions "products.price <= ?", maximum_price unless maximum_price.blank? scope = scope.conditions "products.category_id = ?", category_id unless category_id.blank? scope end 
+7
source

All Articles