diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-05-12 23:29:07 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-05-12 23:29:22 +0100 |
commit | 6e466f17c3d0d06cc364c27fd844a66fb4a89aa2 (patch) | |
tree | b78aab20fc12ab65ae10f4daa56c7fd1b34411bb /activerecord/lib | |
parent | b210d9e722aace63909fb1ebe1b1434ded650ead (diff) | |
download | rails-6e466f17c3d0d06cc364c27fd844a66fb4a89aa2.tar.gz rails-6e466f17c3d0d06cc364c27fd844a66fb4a89aa2.tar.bz2 rails-6e466f17c3d0d06cc364c27fd844a66fb4a89aa2.zip |
Don't use mass-assignment protection when setting foreign keys or association conditions on singular associations. Fixes #481 (again).
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index ea4d73d414..877ddf3ee1 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -18,7 +18,7 @@ module ActiveRecord end def create(attributes = {}, options = {}) - new_record(:create, attributes, options) + build(attributes, options).tap { |record| record.save } end def create!(attributes = {}, options = {}) @@ -26,7 +26,14 @@ module ActiveRecord end def build(attributes = {}, options = {}) - new_record(:build, attributes, options) + record = reflection.build_association + record.assign_attributes( + scoped.scope_for_create.except(klass.primary_key), + :without_protection => true + ) + record.assign_attributes(attributes, options) + set_new_record(record) + record end private @@ -43,13 +50,6 @@ module ActiveRecord def set_new_record(record) replace(record) end - - def new_record(method, attributes, options) - attributes = scoped.scope_for_create.merge(attributes || {}) - record = reflection.send("#{method}_association", attributes, options) - set_new_record(record) - record - end end end end |