I have a ActiveRecord query for example like this:
@result = stuff.limit(10)
where stuff is a active record query with where clauses, order by, etc...
Now I thought why to pass magic numbers like that to the controller? So do you think is it a good practice to define a scope for "limit(10)" and use that instead? and how would the syntax look like?
There are indeed multiple ways of doing such, class methods are one as pointed out by @Dave Newton. If you'd like to use scopes, here's how:
scope :max_records, lambda { |record_limit|
limit(record_limit)
}
Or with the Ruby 1.9 "stabby" lambda syntax and multiple arguments:
scope :max_records, ->(record_limit, foo_name) { # No space between "->" and "("
where(:foo => foo_name).limit(record_limit)
}
If you'd like to know the deeper differences between scopes and class methods, check out this blog post.
Hope it helps. Cheers!
Well Scopes are meant for this
Scoping allows you to specify commonly-used Arel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as where, joins and includes. All scope methods will return an ActiveRecord::Relation object which will allow for further methods (such as other scopes) to be called on it.
Source: http://guides.rubyonrails.org/active_record_querying.html#scopes
So if you feel that there are some common queries which you have, or you need some kind of chaining in your queries which are common to many. Then i will suggest you to go for scopes to prevent repetition.
Now to answer how the scope will look like in your case
class YourModel < ActiveRecord::Base
scope :my_limit, ->(num) { limit(num)}
scope :your_where_condition, ->(num) { where("age > 10").mylimit(num) }
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With