Reputation: 23
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?
Upvotes: 2
Views: 104
Reputation: 10856
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)
Upvotes: 2
Reputation: 15779
You can use the both approaches. But via method it's gonna be more readable when your code for scoping grows.
Upvotes: 3