diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-01-23 21:29:36 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-01-30 11:56:41 +0000 |
commit | 63c73dd0214188dc91442db538e141e30ec3b1b9 (patch) | |
tree | de4c6b5a24c40b6e8f1b6d99b425a4bca77bf070 /activerecord/lib | |
parent | 8adfede96e269ec51f3c52e4e33c8ac63516d546 (diff) | |
download | rails-63c73dd0214188dc91442db538e141e30ec3b1b9.tar.gz rails-63c73dd0214188dc91442db538e141e30ec3b1b9.tar.bz2 rails-63c73dd0214188dc91442db538e141e30ec3b1b9.zip |
We shouldn't be using scoped.scoping { ... } to build associated records, as this can affect validations/callbacks/etc inside the record itself [#6252 state:resolved]
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 13 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 15 |
2 files changed, 14 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 24fb49a65d..a37c3dd432 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -466,17 +466,14 @@ module ActiveRecord force ? record.save! : record.save(:validate => validate) end - def create_record(attrs, &block) + def create_record(attributes, &block) ensure_owner_is_persisted! - - transaction do - scoped.scoping { build_record(attrs, &block) } - end + transaction { build_record(attributes, &block) } end - def build_record(attrs, &block) - attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) - record = @reflection.build_association(attrs) + def build_record(attributes, &block) + attributes = scoped.scope_for_create.merge(attributes) + record = @reflection.build_association(attributes) add_record_to_target_with_callbacks(record, &block) end diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index b6f49c6f36..b43b52fc52 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -2,9 +2,7 @@ module ActiveRecord module Associations class SingularAssociation < AssociationProxy #:nodoc: def create(attributes = {}) - record = scoped.scoping { @reflection.create_association(attributes) } - set_new_record(record) - record + new_record(:create, attributes) end def create!(attributes = {}) @@ -12,9 +10,7 @@ module ActiveRecord end def build(attributes = {}) - record = scoped.scoping { @reflection.build_association(attributes) } - set_new_record(record) - record + new_record(:build, attributes) end private @@ -36,6 +32,13 @@ module ActiveRecord raise_on_type_mismatch(record) if record record end + + def new_record(method, attributes) + attributes = scoped.scope_for_create.merge(attributes || {}) + record = @reflection.send("#{method}_association", attributes) + set_new_record(record) + record + end end end end |