diff options
author | Jon Leighton <j@jonathanleighton.com> | 2010-12-16 22:39:46 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-20 13:56:04 -0800 |
commit | ffa57671bb664a715eb2eebaa7476c747abf0fb1 (patch) | |
tree | 34fe7a3e7ad89bbf488eb638369364db0f4ade20 | |
parent | 379c02267b3cbbf6d1ac48bc79ec6ea01af7b53a (diff) | |
download | rails-ffa57671bb664a715eb2eebaa7476c747abf0fb1.tar.gz rails-ffa57671bb664a715eb2eebaa7476c747abf0fb1.tar.bz2 rails-ffa57671bb664a715eb2eebaa7476c747abf0fb1.zip |
Delete create, create! and create_record from HasManyThroughAssociation in exchange for more generic versions in AssociationCollection
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 26 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 18 |
2 files changed, 14 insertions, 30 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 51d8952eca..3f80133299 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -263,7 +263,7 @@ module ActiveRecord else create_record(attrs) do |record| yield(record) if block_given? - record.save + insert_record(record, false) end end end @@ -271,7 +271,7 @@ module ActiveRecord def create!(attrs = {}) create_record(attrs) do |record| yield(record) if block_given? - record.save! + insert_record(record, true) end end @@ -488,18 +488,20 @@ module ActiveRecord end def create_record(attrs) - attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) ensure_owner_is_persisted! - scoped_where = scoped.where_values_hash - create_scope = scoped_where ? @scope[:create].merge(scoped_where) : @scope[:create] - record = @reflection.klass.send(:with_scope, :create => create_scope) do - @reflection.build_association(attrs) - end - if block_given? - add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } - else - add_record_to_target_with_callbacks(record) + attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) + create_scope = @scope[:create].merge(scoped.where_values_hash || {}) + + transaction do + record = with_scope(:create => create_scope) do + @reflection.build_association(attrs) + end + if block_given? + add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } + else + add_record_to_target_with_callbacks(record) + end end end diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 179a27725b..f0bc6aedf2 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -9,14 +9,6 @@ module ActiveRecord alias_method :new, :build - def create!(attrs = nil) - create_record(attrs, true) - end - - def create(attrs = nil) - create_record(attrs, false) - end - def destroy(*records) transaction do delete_records(flatten_deeper(records)) @@ -35,16 +27,6 @@ module ActiveRecord end protected - def create_record(attrs, force = true) - ensure_owner_is_persisted! - - transaction do - object = @reflection.klass.new(attrs) - add_record_to_target_with_callbacks(object) {|r| insert_record(object, force) } - object - end - end - def target_reflection_has_associated_record? if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.primary_key_name].blank? false |