diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 23 | ||||
-rw-r--r-- | activerecord/test/connections/native_mysql/connection.rb | 3 |
3 files changed, 20 insertions, 8 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index fb78e6e539..e64e594ad2 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add AssociationCollection#create! to be consistent with AssociationCollection#create when dealing with a foreign key that is a protected attribute [Cody Fauser] + * Added counter optimization for AssociationCollection#any? so person.friends.any? won't actually load the full association if we have the count in a cheaper form [DHH] * Subclasses of an abstract class work with single-table inheritance. #5704 [nick+rails@ag.arizona.edu, Ryan Davis, Jeremy Kemper] 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 diff --git a/activerecord/test/connections/native_mysql/connection.rb b/activerecord/test/connections/native_mysql/connection.rb index 9a91a75cac..ff6bda51bb 100644 --- a/activerecord/test/connections/native_mysql/connection.rb +++ b/activerecord/test/connections/native_mysql/connection.rb @@ -4,6 +4,9 @@ require 'logger' ActiveRecord::Base.logger = Logger.new("debug.log") +# GRANT ALL PRIVILEGES ON activerecord_unittest.* to 'rails'@'localhost'; +# GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost'; + ActiveRecord::Base.configurations = { 'arunit' => { :adapter => 'mysql', |