Class: AssociationScope::Scope::HasAndBelongsToManyReflection

Inherits:
Scope
  • Object
show all
Defined in:
lib/association_scope/scope/has_and_belongs_to_many_reflection.rb

Instance Method Summary collapse

Instance Method Details

#apply ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/association_scope/scope/has_and_belongs_to_many_reflection.rb', line 6

def apply
  association = @association.pluralize
  class_name = reflection_details.klass
  inverse_association = inverse_association(class_name)
  validate_scope!(reflection_details)
  association_scope = reflection_details.scope
  owner_table = model.table_name

  raise AssociationMissingError.new(missing_in: class_name, association: model.table_name) unless inverse_association

  model.scope association, -> do
    Scope.target_relation(class_name, association_scope)
      .joins(inverse_association)
      .where(owner_table => self)
      .distinct
  end
end