diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-19 09:13:00 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-19 09:13:00 +0000 |
commit | 81b05fd9098c492bcf8651fe9042e5157d9a994b (patch) | |
tree | bb8328358e9be72e8cbb3edbba69a3f50c1170ee /activerecord/lib | |
parent | a752fdb321d116c1f4fa0a61bd5d633575f0b09c (diff) | |
download | rails-81b05fd9098c492bcf8651fe9042e5157d9a994b.tar.gz rails-81b05fd9098c492bcf8651fe9042e5157d9a994b.tar.bz2 rails-81b05fd9098c492bcf8651fe9042e5157d9a994b.zip |
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
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 18 |
1 files changed, 13 insertions, 5 deletions
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, |