Say there are 3 models: A, B, and C. Each of these models has the x
attribute.
Is that possible to define a named scope in a module and include this module in A, B, and C ?
I tried to do so and got an error message saying that scope
is not recognized...
As for Rails 4.x you can use gem scopes_rails
It can generate
scopes
file and include it to your model.Also, it can automatically generate scopes for
state_machines
states.Yes it is
As of Rails 3.1 the syntax is simplified a little by ActiveSupport::Concern:
Now you can do
ActiveSupport::Concern also sweeps in the dependencies of the included module, here is the documentation
[update, addressing aceofbassgreg's comment]
The Rails 3.1 and later ActiveSupport::Concern allows an include module's instance methods to be included directly, so that it's not necessary to create an InstanceMethods module inside the included module. Also it's no longer necessary in Rails 3.1 and later to include M::InstanceMethods and extend M::ClassMethods. So we can have simpler code like this: