Class: NumberedRelationships::HasAndBelongsToManyConstructor

Inherits:
Constructor
  • Object
show all
Defined in:
lib/numbered_relationships/has_and_belongs_to_many.rb

Instance Method Summary collapse

Methods inherited from Constructor

#initialize

Constructor Details

This class inherits a constructor from NumberedRelationships::Constructor

Instance Method Details

#constructObject



5
6
7
8
9
# File 'lib/numbered_relationships/has_and_belongs_to_many.rb', line 5

def construct
  super
  return construct_habtm_with_join_table if join_table
  construct_habtm_without_join_table
end

#construct_habtm_with_join_tableObject



11
12
13
14
15
# File 'lib/numbered_relationships/has_and_belongs_to_many.rb', line 11

def construct_habtm_with_join_table
  @klass.joins(association)
        .group("#{table}.id")
        .having("count(#{join_table}.#{foreign_key}) #{@operator} #{@n}")
end

#construct_habtm_without_join_tableObject



17
18
19
20
21
# File 'lib/numbered_relationships/has_and_belongs_to_many.rb', line 17

def construct_habtm_without_join_table
  @klass.joins(association)
        .group("#{table}.id")
        .having("count(#{foreign_key}) #{@operator} #{@n}")
end