Instead of named scope or scope, we can write a class method.
ex:
def self.desc
<code>
end
Why would we ever use a scope?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
We can certainly write class methods which work just like scopes. They are also a bit of a holdover from Rails 2.x when we didn’t have arel to play with.
To me scopes are a way of “defining” the model like the associations do. Scopes make defining these things nicer and cleaner, and they generally go at the top so you can see the model at a glance.
I’ve found I tend to move towards class methods when I need to start passing in arguments. Yeah, there’s a way to do it, but really at that point you’re starting to want a class method (especially if there are optional arguments, though I believe Ruby 1.9 goes some way toward fixing that)