aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/through_association.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2015-01-02 14:29:07 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2015-01-02 14:29:07 -0300
commitecdae459d209ae5144fd4390e8a6f247d7f98a1c (patch)
tree63541ab762c7e1d7d9611ab60d7a248bc8360c4b /activerecord/lib/active_record/associations/through_association.rb
parent2d743b528cf8ee2a8dc016a5dd0d54bf7cbc34e4 (diff)
parent1e0130f7e0a27878dd78ed1335ae8e6fbc52397f (diff)
downloadrails-ecdae459d209ae5144fd4390e8a6f247d7f98a1c.tar.gz
rails-ecdae459d209ae5144fd4390e8a6f247d7f98a1c.tar.bz2
rails-ecdae459d209ae5144fd4390e8a6f247d7f98a1c.zip
Merge pull request #15309 from iantropov/issue_12698_build_through
Add setting of FK for throgh associations while building Conflicts: activerecord/CHANGELOG.md activerecord/test/cases/associations/has_many_through_associations_test.rb
Diffstat (limited to 'activerecord/lib/active_record/associations/through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/through_association.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb
index e47e81aa0f..09828dbd9b 100644
--- a/activerecord/lib/active_record/associations/through_association.rb
+++ b/activerecord/lib/active_record/associations/through_association.rb
@@ -91,6 +91,17 @@ module ActiveRecord
raise HasManyThroughNestedAssociationsAreReadonly.new(owner, reflection)
end
end
+
+ def build_record(attributes)
+ inverse = source_reflection.inverse_of
+ target = through_association.target
+
+ if inverse && target && !target.is_a?(Array)
+ attributes[inverse.foreign_key] = target.id
+ end
+
+ super(attributes)
+ end
end
end
end