Answer the question
In order to leave comments, you need to log in
Whether it is possible to make scope for association (or as that with to imitate)?
class User < ActiveRecord::Base
has_and_belongs_to_many :roles, -> { uniq }
has_and_belongs_to_many :additional_roles, :class_name => 'Role'
Answer the question
In order to leave comments, you need to log in
The scope can be made for any SQL, but there is a reasonable convention - the scope should produce objects of the class in which it is defined. Those. in your case, you need to define the scope in the Roles class and pass the user for which you want to filter the roles as a parameter. Well, keep in mind that the scope is a class method and not an object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question