diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-13 17:45:51 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-13 17:45:51 -0300 |
commit | b0f07abbe385463cb03cfc890b85bb8ce3d5ccef (patch) | |
tree | 79db08aab556ace8f5bee62310fbcb77b07f478c /activerecord/lib | |
parent | 497def80b53db73712a6abf2c066e9bc4980ccd2 (diff) | |
parent | 0a4e3f4a7f6d93c713389140bc6e6613b721b0ff (diff) | |
download | rails-b0f07abbe385463cb03cfc890b85bb8ce3d5ccef.tar.gz rails-b0f07abbe385463cb03cfc890b85bb8ce3d5ccef.tar.bz2 rails-b0f07abbe385463cb03cfc890b85bb8ce3d5ccef.zip |
Merge pull request #15579 from sgrif/sg-through-associations
Through associations should set both parent ids on join models
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/through_association.rb | 16 |
2 files changed, 15 insertions, 7 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 da9b125fd6..1713ab7ed3 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -180,7 +180,11 @@ module ActiveRecord def through_records_for(record) attributes = construct_join_attributes(record) candidates = Array.wrap(through_association.target) - candidates.find_all { |c| c.attributes.slice(*attributes.keys) == attributes } + candidates.find_all do |c| + attributes.all? do |key, value| + c.public_send(key) == value + end + end end def delete_through_records(records) diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index fcf3b219d4..f00fef8b9e 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -41,12 +41,16 @@ module ActiveRecord def construct_join_attributes(*records) ensure_mutable - join_attributes = { - source_reflection.foreign_key => - records.map { |record| - record.send(source_reflection.association_primary_key(reflection.klass)) - } - } + if source_reflection.association_primary_key(reflection.klass) == reflection.klass.primary_key + join_attributes = { source_reflection.name => records } + else + join_attributes = { + source_reflection.foreign_key => + records.map { |record| + record.send(source_reflection.association_primary_key(reflection.klass)) + } + } + end if options[:source_type] join_attributes[source_reflection.foreign_type] = |