diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-02-14 00:14:16 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-02-14 01:40:31 +0000 |
commit | 7ce7ae0c8bd94e7f96c1448183b83da85ef91edb (patch) | |
tree | bbb92ebdf7b3c27a699c213285f356f9d89bf261 | |
parent | eab5fb49f839bf19da4c31b35142f9e05d05ed2e (diff) | |
download | rails-7ce7ae0c8bd94e7f96c1448183b83da85ef91edb.tar.gz rails-7ce7ae0c8bd94e7f96c1448183b83da85ef91edb.tar.bz2 rails-7ce7ae0c8bd94e7f96c1448183b83da85ef91edb.zip |
Get rid of AssociationCollection#save_record
5 files changed, 26 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 8e006e4d9d..4ead67b282 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -197,16 +197,15 @@ module ActiveRecord else create_record(attrs) do |record| yield(record) if block_given? - insert_record(record, false) + insert_record(record) end end end - def create!(attrs = {}) - create_record(attrs) do |record| - yield(record) if block_given? - insert_record(record, true) - end + def create!(attrs = {}, &block) + record = create(attrs, &block) + Array.wrap(record).each(&:save!) + record end # Returns the size of the collection by executing a SELECT COUNT(*) @@ -419,17 +418,11 @@ module ActiveRecord end + existing end - # Do the relevant stuff to insert the given record into the association collection. The - # force param specifies whether or not an exception should be raised on failure. The - # validate param specifies whether validation should be performed (if force is false). - def insert_record(record, force = true, validate = true) + # Do the relevant stuff to insert the given record into the association collection. + def insert_record(record, validate = true) raise NotImplementedError end - def save_record(record, force, validate) - force ? record.save! : record.save(:validate => validate) - end - def create_record(attributes, &block) ensure_owner_is_persisted! transaction { build_record(attributes, &block) } diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index b5ec45159b..b9c9919e7a 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -11,10 +11,8 @@ module ActiveRecord protected - def insert_record(record, force = true, validate = true) - if record.new_record? - return false unless save_record(record, force, validate) - end + def insert_record(record, validate = true) + return if record.new_record? && !record.save(:validate => validate) if @reflection.options[:insert_sql] @owner.connection.insert(interpolate(@reflection.options[:insert_sql], record)) @@ -27,7 +25,7 @@ module ActiveRecord @owner.connection.insert stmt.to_sql end - true + record end def association_scope diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 0b246587c0..543b073393 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -8,9 +8,9 @@ module ActiveRecord class HasManyAssociation < AssociationCollection #:nodoc: protected - def insert_record(record, force = false, validate = true) + def insert_record(record, validate = true) set_owner_attributes(record) - save_record(record, force, validate) + record.save(:validate => validate) end private 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 f299f51466..9f74d57c4d 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -22,12 +22,21 @@ module ActiveRecord end end + def <<(*records) + unless @owner.new_record? + records.flatten.each do |record| + raise_on_type_mismatch(record) + record.save! if record.new_record? + end + end + + super + end + protected - def insert_record(record, force = true, validate = true) - if record.new_record? - return unless save_record(record, force, validate) - end + def insert_record(record, validate = true) + return if record.new_record? && !record.save(:validate => validate) through_association = @owner.send(@reflection.through_reflection.name) through_association.create!(construct_join_attributes(record)) diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index c52af23fbd..95357ee5dc 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -312,7 +312,7 @@ module ActiveRecord association.destroy(record) elsif autosave != false && (@new_record_before_save || record.new_record?) if autosave - saved = association.send(:insert_record, record, false, false) + saved = association.send(:insert_record, record, false) else association.send(:insert_record, record) end |