From 5d6d669bfe1e480dd4d0cc5042b7faba4b469846 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 14 Feb 2011 01:19:42 +0000 Subject: Inline ensure_owner_is_persisted! as it is only called from one place --- .../lib/active_record/associations/association_collection.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 8b5e600c96..888ebdf7af 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -67,11 +67,13 @@ module ActiveRecord end def create(attributes = {}) + unless @owner.persisted? + raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" + end + if attributes.is_a?(Array) attributes.collect { |attr| create(attr) } else - ensure_owner_is_persisted! - transaction do add_to_target(build_record(attributes)) do |record| yield(record) if block_given? @@ -471,12 +473,6 @@ module ActiveRecord @owner.class.send(full_callback_name.to_sym) || [] end - def ensure_owner_is_persisted! - unless @owner.persisted? - raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" - end - end - # Should we deal with assoc.first or assoc.last by issuing an independent query to # the database, or by getting the target, and then taking the first/last item from that? # -- cgit v1.2.3