Skip to content

Commit

Permalink
prevent mass assignment of polymorphic type when using build
Browse files Browse the repository at this point in the history
Closes #8265
  • Loading branch information
Yves Senn committed Nov 22, 2012
1 parent 293c121 commit 053bfa2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##

* Prevent mass assignment to the type column of polymorphic associations when using `build`
Fix #8265

*Yves Senn*

* Fix postgresql adapter to handle BC timestamps correctly

HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years)
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/associations/association.rb
Expand Up @@ -232,7 +232,8 @@ def stale_state

def build_record(attributes)
reflection.build_association(attributes) do |record|
attributes = create_scope.except(*(record.changed - [reflection.foreign_key]))
skip_assign = [reflection.foreign_key, reflection.type].compact
attributes = create_scope.except(*(record.changed - skip_assign))
record.assign_attributes(attributes)
end
end
Expand Down
Expand Up @@ -1579,6 +1579,14 @@ def test_abstract_class_with_polymorphic_has_many
assert_equal [tagging], post.taggings
end

def test_build_with_polymotphic_has_many_does_not_allow_to_override_type_and_id
welcome = posts(:welcome)
tagging = welcome.taggings.build(:taggable_id => 99, :taggable_type => 'ShouldNotChange')

assert_equal welcome.id, tagging.taggable_id
assert_equal 'Post', tagging.taggable_type
end

def test_dont_call_save_callbacks_twice_on_has_many
firm = companies(:first_firm)
contract = firm.contracts.create!
Expand Down

0 comments on commit 053bfa2

Please sign in to comment.