According to the Thoughbot Rails Style Guide, we should
Use def self.method, not the scope :method DSL.
My question is: is this simply for the sake of choosing one of these styles and sticking with it for consistency, or is there another reason I’m missing?
Regardless of whether or not you would adhere to this style guide, I was just curious.
1
This is a great article discussing the difference:
http://blog.plataformatec.com.br/2013/02/active-record-scopes-vs-class-methods/
TLDR:
- Scopes are always chainable.
- Scopes always return a relation.
- Scopes look a bit cleaner when you extend them.
I’m currently working on a big project with 6 developers. We have a lot of models and I noticed that if you have like 5 or 6 scopes things start to look cluttered. If we are using the self. syntax things look a lot cleaner then a lambda.
So I guess 2 things:
– looks cleaner
– consistency (like you said)
Note that this post is solely based on personal experience. I could be wrong. 🙂