Class: ActiveRecord::Associations::HasAndBelongsToManyAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_primary_keys/associations.rb

Instance Method Summary collapse

Instance Method Details

#construct_sqlObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/composite_primary_keys/associations.rb', line 33

def construct_sql
  if @reflection.options[:finder_sql]
    @finder_sql = interpolate_sql(@reflection.options[:finder_sql])
  else
    # CPK
    # @finder_sql = "#{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{owner_quoted_id} "
    @finder_sql = full_columns_equals(@reflection.options[:join_table], @reflection.cpk_primary_key, owner_quoted_id)
    @finder_sql << " AND (#{conditions})" if conditions
  end

  join_condition = if composite?
    conditions = Array.new
    primary_keys.length.times do |i|
      conditions << "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key[i]} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key[i]}"
    end
    conditions.join(' AND ')
  else
    "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}"
  end
  #@join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}"
  @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON (#{join_condition})"

  construct_counter_sql
end