From 1e0130f7e0a27878dd78ed1335ae8e6fbc52397f Mon Sep 17 00:00:00 2001 From: Ivan Antropov Date: Sun, 25 May 2014 21:08:11 +0700 Subject: Add setting of FK for throgh associations while building --- .../lib/active_record/associations/through_association.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index f8a85b8a6f..9de9875b38 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -84,6 +84,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 -- cgit v1.2.3