From 9a7dbe2c0570e11b9033df735c937d5f5416e0ca Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 10 May 2011 22:47:54 +0100 Subject: Don't use mass-assignment protection when applying the scoped.scope_for_create. Fixes #481. --- .../lib/active_record/associations/collection_association.rb | 6 ++++-- .../lib/active_record/associations/through_association.rb | 4 +++- .../test/cases/associations/has_many_associations_test.rb | 9 +++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 85a4f47b7d..59e6e00ad1 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -99,7 +99,6 @@ module ActiveRecord else add_to_target(build_record(attributes, options)) do |record| yield(record) if block_given? - set_owner_attributes(record) end end end @@ -423,7 +422,10 @@ module ActiveRecord end def build_record(attributes, options) - reflection.build_association(scoped.scope_for_create.merge(attributes || {}), options) + record = reflection.build_association + record.assign_attributes(scoped.scope_for_create, :without_protection => true) + record.assign_attributes(attributes || {}, options) + record end def delete_or_destroy(records, method) diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index e6ab628719..e436fef46d 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -14,7 +14,9 @@ module ActiveRecord def target_scope scope = super chain[1..-1].each do |reflection| - scope = scope.merge(reflection.klass.scoped) + # Discard the create with value, as we don't want that the affect the objects we + # create on the association + scope = scope.merge(reflection.klass.scoped.create_with(nil)) end scope end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index dc2481456b..5a414c49f1 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -86,11 +86,20 @@ class HasManyAssociationsTest < ActiveRecord::TestCase bulb = car.bulbs.new assert_equal car.id, bulb.car_id + bulb = car.bulbs.new :car_id => car.id + 1 + assert_equal car.id, bulb.car_id + bulb = car.bulbs.build assert_equal car.id, bulb.car_id + bulb = car.bulbs.build :car_id => car.id + 1 + assert_equal car.id, bulb.car_id + bulb = car.bulbs.create assert_equal car.id, bulb.car_id + + bulb = car.bulbs.create :car_id => car.id + 1 + assert_equal car.id, bulb.car_id ensure Bulb.attr_protected :id end -- cgit v1.2.3