Is normal or a best practice to have chained scopes in controllers actions?

Hey, there!

I’m pretty new to Rails so this may be a really stupid question but, I
googled it and found just one specific reference
(Say no to chained scopes!).
In the application I’m working on there are some controller actions
chaining scopes like this:

@results =

Is it normal? There are best practices on chaining scopes like: you can
chain maximum 2 scopes?


Composing scopes is completely normal - they are clear, easy to test and
can serve as building blocks. What you need to keep in mind is to keep
code DRY.

For example, if you have an e-commerce site selling guitars and from the
data you see that high-end left-handed guitars are most often bough by
left-handed adults then you don’t want to litter the code with multiple
occurrences of This is an
detail. You should define a scope (or a class method)
Person.best_high_end_left_handed_buyers and define it to equal If requirements change at a later point
time you can just change the definition. (Alternatively you can define a
private method that returns the scope in your controller).

TL;DR: It’s normal to chain scopes but keep your code DRY and make your
code express your intent.

Greg Navis
I help tech companies to scale Heroku-hosted Rails apps.
Free, biweekly scalability newsletter for SaaS CEOs

I think we’re missing DRY but this is the answer I was looking for!

Em quinta-feira, 19 de maio de 2016 06:06:20 UTC-3, Greg Navis escreveu: