diff options
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 12 |
1 files changed, 4 insertions, 8 deletions
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? # |