diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-02 14:29:07 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-02 14:29:07 -0300 |
commit | ecdae459d209ae5144fd4390e8a6f247d7f98a1c (patch) | |
tree | 63541ab762c7e1d7d9611ab60d7a248bc8360c4b /activerecord/lib | |
parent | 2d743b528cf8ee2a8dc016a5dd0d54bf7cbc34e4 (diff) | |
parent | 1e0130f7e0a27878dd78ed1335ae8e6fbc52397f (diff) | |
download | rails-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')
-rw-r--r-- | activerecord/lib/active_record/associations/through_association.rb | 11 |
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 |