From ffa57671bb664a715eb2eebaa7476c747abf0fb1 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 16 Dec 2010 22:39:46 +0000 Subject: Delete create, create! and create_record from HasManyThroughAssociation in exchange for more generic versions in AssociationCollection --- .../associations/association_collection.rb | 26 ++++++++++++---------- .../associations/has_many_through_association.rb | 18 --------------- 2 files changed, 14 insertions(+), 30 deletions(-) (limited to 'activerecord') 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 -- cgit v1.2.3