GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
In the case of a model with a default_scope, includes, and other scopes, what should the order be? Includes, then default scope, then scopes below in the 'other macros' section would be my guess. Also, in my opinion, scopes should be first-class citizens in the macro-ordering section, along with attribute macros, validations, etc.
Accidentally sent that last comment before it was finished!
Here's the revision:
I prefer the following order:
Callbacks should be listed in the order they happen:
An overall example:
CONFIG = 'something'
before_save :do_before_save, :and_something_else
scope :some_category, where(something: false)
scope :another_category, where(something: true)
validate :attribute, presence: true
delegate :something, to: :another_model
# The rest of the built in stuff not named
# Custom stuff
what about default_scope? just group with other scopes?
I don't often use default scopes, but if I were to I would probably make it the first in the list of scopes.
default_scope where(deleted: false)
scope :deleted, unscoped.where(deleted: true)
scope :another, where(something: true)
That's what I do currently, but that conflicts with https://github.com/bbatsov/rails-style-guide#activerecord.
I don't see any scope calls in that example (other than the default_scope), where is it suggested they should go?
We could at least specify where regular scope calls go in relation to the default_scope.
That's the point, it only says to put default_scope at the top, but doesn't specify where the other scopes should go. That's what I'm trying to flesh out here.
I'd like to know how one is to order scopes.