From 0ee0c1b2aae3cb90869c79235470e6b69296feeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=BCtke?= Date: Tue, 5 Dec 2006 17:55:44 +0000 Subject: Add AssociationCollection#create! to be consistent with AssociationCollection#create when dealing with a foreign key that is a protected attribute git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5677 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations/association_collection.rb | 23 ++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 273cc64334..b8337fe4d9 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -86,14 +86,11 @@ module ActiveRecord end def create(attributes = {}) - # Can't use Base.create since the foreign key may be a protected attribute. - if attributes.is_a?(Array) - attributes.collect { |attr| create(attr) } - else - record = build(attributes) - record.save unless @owner.new_record? - record - end + build_and_save_with(attributes, :save) + end + + def create!(attributes = {}) + build_and_save_with(attributes, :save!) end # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and @@ -202,6 +199,16 @@ module ActiveRecord @owner.class.read_inheritable_attribute(full_callback_name.to_sym) || [] end + def build_and_save_with(attributes, method) + # Can't use Base.create since the foreign key may be a protected attribute. + if attributes.is_a?(Array) + attributes.collect { |attr| create(attr) } + else + record = build(attributes) + record.send(method) unless @owner.new_record? + record + end + end end end end -- cgit v1.2.3