Class: ActiveSimilar::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_similar/association.rb

Overview

Builds association subquery.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, klass, other) ⇒ Association

Returns a new instance of Association.

Parameters:

  • name (Symbol)
  • klass (ActiveRecord::Base)
  • other (Integer ActiveRecord::Base)


20
21
22
23
24
# File 'lib/active_similar/association.rb', line 20

def initialize(name, klass, other)
  @klass      = klass
  @reflection = klass.reflect_on_association(name)
  @other      = other
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/active_similar/association.rb', line 6

def klass
  @klass
end

#otherObject (readonly)

Returns the value of attribute other.



6
7
8
# File 'lib/active_similar/association.rb', line 6

def other
  @other
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



6
7
8
# File 'lib/active_similar/association.rb', line 6

def reflection
  @reflection
end

Instance Method Details

#scopeActiveRecord::Relation

Returns the association join scope.

Returns:

  • (ActiveRecord::Relation)


28
29
30
31
32
# File 'lib/active_similar/association.rb', line 28

def scope
  klass
    .joins(through_name)
    .where(through_table_name => { foreign_key => subquery })
end