Ruby on Rails Thursday, May 19, 2016

I think we're missing DRY but this is the answer I was looking for! Thanks, Greg!

Em quinta-feira, 19 de maio de 2016 06:06:20 UTC-3, Greg Navis escreveu:
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 your 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 male left-handed adults then you don't want to litter the code with multiple occurrences of `Person.male.adult.left_handed`. This is an implementation detail. You should define a scope (or a class method) `Person.best_high_end_left_handed_buyers` and define it to equal `Person.male.adult.left_handed`. If requirements change at a later point in 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.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/53fbdfad-3adf-470c-a6fd-7fece4fbc8ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment