diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-08 16:04:03 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-06-13 14:38:34 -0600 |
commit | 0a4e3f4a7f6d93c713389140bc6e6613b721b0ff (patch) | |
tree | 393f55354f3eb63dde582612a7b26dd5e929ffbf /activerecord/lib | |
parent | 49fee3d271e52a44a7bc7fcbbcb00792b613b7df (diff) | |
download | rails-0a4e3f4a7f6d93c713389140bc6e6613b721b0ff.tar.gz rails-0a4e3f4a7f6d93c713389140bc6e6613b721b0ff.tar.bz2 rails-0a4e3f4a7f6d93c713389140bc6e6613b721b0ff.zip |
Through associations should set both parent ids on join models
member = Member.new(club: Club.new)
member.save!
Before:
member.current_membership.club_id # => nil
After:
member.current_membership.club_id # => club's id
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] = |