From 81b05fd9098c492bcf8651fe9042e5157d9a994b Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 19 Aug 2006 09:13:00 +0000 Subject: Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key. Closes #5815. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4790 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations/has_many_through_association.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 7662e31914..b5feeff1d2 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -53,7 +53,7 @@ module ActiveRecord klass.transaction do args.each do |associate| raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record? - klass.with_scope(:create => construct_association_attributes(through)) { klass.create! } + klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! } end end end @@ -99,7 +99,8 @@ module ActiveRecord @reflection.options[:uniq] ? records.to_set.to_a : records end - def construct_association_attributes(reflection) + # Construct attributes for associate pointing to owner. + def construct_owner_attributes(reflection) if as = reflection.options[:as] { "#{as}_id" => @owner.id, "#{as}_type" => @owner.class.base_class.name.to_s } @@ -108,7 +109,13 @@ module ActiveRecord end end - def construct_quoted_association_attributes(reflection) + # Construct attributes for :through pointing to owner and associate. + def construct_join_attributes(associate) + construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.association_foreign_key => associate.id) + end + + # Associate attributes pointing to owner, quoted. + def construct_quoted_owner_attributes(reflection) if as = reflection.options[:as] { "#{as}_id" => @owner.quoted_id, "#{as}_type" => reflection.klass.quote( @@ -119,9 +126,10 @@ module ActiveRecord end end + # Build SQL conditions from attributes, qualified by table name. def construct_conditions table_name = @reflection.through_reflection.table_name - conditions = construct_quoted_association_attributes(@reflection.through_reflection).map do |attr, value| + conditions = construct_quoted_owner_attributes(@reflection.through_reflection).map do |attr, value| "#{table_name}.#{attr} = #{value}" end conditions << sql_conditions if sql_conditions @@ -161,7 +169,7 @@ module ActiveRecord end def construct_scope - { :create => construct_association_attributes(@reflection), + { :create => construct_owner_attributes(@reflection), :find => { :from => construct_from, :conditions => construct_conditions, :joins => construct_joins, -- cgit v1.2.3