1

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
  • I would say it's a matter of preference. Even in rails sources you can find this "# Note that this is simply 'syntactic sugar' for defining an actual # class method:" There's not much difference between those two. Both create class level method.
    – Greg
    Oct 22, 2015 at 8:39

2 Answers 2

2

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.
1

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. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.