AutosaveAssociation is a module that takes care of automatically saving associacted records when their parent is saved. In addition to saving, it also destroys any associated records that were marked for destruction. (See mark_for_destruction and marked_for_destruction?).
Saving of the parent, its associations, and the destruction of marked associations, all happen inside a transaction. This should never leave the database in an inconsistent state.
If validations for any of the associations fail, their error messages will be applied to the parent.
Note that it also means that associations marked for destruction won’t be destroyed directly. They will however still be marked for destruction.
Note that :autosave => false is not same as not declaring :autosave. When the :autosave option is not present new associations are saved.
class Post has_one :author, :autosave => true end
Saving changes to the parent and its associated model can now be performed automatically and atomically:
post = Post.find(1) post.title # => "The current global position of migrating ducks" post.author.name # => "alloy" post.title = "On the migration of ducks" post.author.name = "Eloy Duran" post.save post.reload post.title # => "On the migration of ducks" post.author.name # => "Eloy Duran"
Destroying an associated model, as part of the parent’s save action, is as simple as marking it for destruction:
post.author.mark_for_destruction post.author.marked_for_destruction? # => true
Note that the model is not yet removed from the database:
id = post.author.id Author.find_by_id(id).nil? # => false post.save post.reload.author # => nil
Now it is removed from the database:
Author.find_by_id(id).nil? # => true
When :autosave is not declared new children are saved when their parent is saved:
class Post has_many :comments # :autosave option is no declared end post = Post.new(:title => 'ruby rocks') post.comments.build(:body => 'hello world') post.save # => saves both post and comment post = Post.create(:title => 'ruby rocks') post.comments.build(:body => 'hello world') post.save # => saves both post and comment post = Post.create(:title => 'ruby rocks') post.comments.create(:body => 'hello world') post.save # => saves both post and comment
When :autosave is true all children is saved, no matter whether they are new records:
class Post has_many :comments, :autosave => true end post = Post.create(:title => 'ruby rocks') post.comments.create(:body => 'hello world') post.comments[0].body = 'hi everyone' post.save # => saves both post and comment, with 'hi everyone' as title
Destroying one of the associated models as part of the parent’s save action is as simple as marking it for destruction:
post.comments.last.mark_for_destruction post.comments.last.marked_for_destruction? # => true post.comments.length # => 2
Note that the model is not yet removed from the database:
id = post.comments.last.id Comment.find_by_id(id).nil? # => false post.save post.reload.comments.length # => 1
Now it is removed from the database:
Comment.find_by_id(id).nil? # => true
Children records are validated unless :validate is false.
Returns whether or not this record has been changed in any way (including whether any of its nested autosave associations are likewise changed)
# File lib/active_record/autosave_association.rb, line 210 210: def changed_for_autosave? 211: !persisted? || changed? || marked_for_destruction? || nested_records_changed_for_autosave? 212: end
Marks this record to be destroyed as part of the parents save transaction. This does not actually destroy the record instantly, rather child record will be destroyed when parent.save is called.
Only useful if the :autosave option on the parent is enabled for this associated model.
# File lib/active_record/autosave_association.rb, line 197 197: def mark_for_destruction 198: @marked_for_destruction = true 199: end
Returns whether or not this record will be destroyed as part of the parents save transaction.
Only useful if the :autosave option on the parent is enabled for this associated model.
# File lib/active_record/autosave_association.rb, line 204 204: def marked_for_destruction? 205: @marked_for_destruction 206: end
Returns the record for an association collection that should be validated or saved. If autosave is false only new records will be returned, unless the parent is/was a new record itself.
# File lib/active_record/autosave_association.rb, line 219 219: def associated_records_to_validate_or_save(association, new_record, autosave) 220: if new_record 221: association 222: elsif autosave 223: association.target.find_all { |record| record.changed_for_autosave? } 224: else 225: association.target.find_all { |record| !record.persisted? } 226: end 227: end
Returns whether or not the association is valid and applies any errors to the parent, self, if it wasn’t. Skips any :autosave enabled records if they’re marked_for_destruction? or destroyed.
# File lib/active_record/autosave_association.rb, line 260 260: def association_valid?(reflection, association) 261: return true if association.destroyed? || association.marked_for_destruction? 262: 263: unless valid = association.valid? 264: if reflection.options[:autosave] 265: association.errors.each do |attribute, message| 266: attribute = "#{reflection.name}.#{attribute}" 267: errors[attribute] << message 268: errors[attribute].uniq! 269: end 270: else 271: errors.add(reflection.name) 272: end 273: end 274: valid 275: end
Is used as a before_save callback to check while saving a collection association whether or not the parent was a new record before saving.
# File lib/active_record/autosave_association.rb, line 279 279: def before_save_collection_association 280: @new_record_before_save = !persisted? 281: true 282: end
go through nested autosave associations that are loaded in memory (without loading any new ones), and return true if is changed for autosave
# File lib/active_record/autosave_association.rb, line 231 231: def nested_records_changed_for_autosave? 232: self.class.reflect_on_all_autosave_associations.any? do |reflection| 233: association = association_instance_get(reflection.name) 234: association && Array.wrap(association.target).any? { |a| a.changed_for_autosave? } 235: end 236: end
Saves the associated record if it’s new or :autosave is enabled.
In addition, it will destroy the association if it was marked for destruction.
# File lib/active_record/autosave_association.rb, line 350 350: def save_belongs_to_association(reflection) 351: if (association = association_instance_get(reflection.name)) && !association.destroyed? 352: autosave = reflection.options[:autosave] 353: 354: if autosave && association.marked_for_destruction? 355: association.destroy 356: elsif autosave != false 357: saved = association.save(:validate => !autosave) if !association.persisted? || autosave 358: 359: if association.updated? 360: association_id = association.send(reflection.options[:primary_key] || :id) 361: self[reflection.primary_key_name] = association_id 362: end 363: 364: saved if autosave 365: end 366: end 367: end
Saves any new associated records, or all loaded autosave associations if :autosave is enabled on the association.
In addition, it destroys all children that were marked for destruction with mark_for_destruction.
This all happens inside a transaction, if the Transactions module is included into ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
# File lib/active_record/autosave_association.rb, line 292 292: def save_collection_association(reflection) 293: if association = association_instance_get(reflection.name) 294: autosave = reflection.options[:autosave] 295: 296: if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave) 297: records.each do |record| 298: next if record.destroyed? 299: 300: if autosave && record.marked_for_destruction? 301: association.destroy(record) 302: elsif autosave != false && (@new_record_before_save || !record.persisted?) 303: if autosave 304: saved = association.send(:insert_record, record, false, false) 305: else 306: association.send(:insert_record, record) 307: end 308: elsif autosave 309: saved = record.save(:validate => false) 310: end 311: 312: raise ActiveRecord::Rollback if saved == false 313: end 314: end 315: 316: # reconstruct the SQL queries now that we know the owner's id 317: association.send(:construct_sql) if association.respond_to?(:construct_sql) 318: end 319: end
Saves the associated record if it’s new or :autosave is enabled on the association.
In addition, it will destroy the association if it was marked for destruction with mark_for_destruction.
This all happens inside a transaction, if the Transactions module is included into ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
# File lib/active_record/autosave_association.rb, line 329 329: def save_has_one_association(reflection) 330: if (association = association_instance_get(reflection.name)) && !association.target.nil? && !association.destroyed? 331: autosave = reflection.options[:autosave] 332: 333: if autosave && association.marked_for_destruction? 334: association.destroy 335: else 336: key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id 337: if autosave != false && (!persisted? || !association.persisted? || association[reflection.primary_key_name] != key || autosave) 338: association[reflection.primary_key_name] = key 339: saved = association.save(:validate => !autosave) 340: raise ActiveRecord::Rollback if !saved && autosave 341: saved 342: end 343: end 344: end 345: end
Validate the associated records if :validate or :autosave is turned on for the association specified by reflection.
# File lib/active_record/autosave_association.rb, line 249 249: def validate_collection_association(reflection) 250: if association = association_instance_get(reflection.name) 251: if records = associated_records_to_validate_or_save(association, !persisted?, reflection.options[:autosave]) 252: records.each { |record| association_valid?(reflection, record) } 253: end 254: end 255: end
Validate the association if :validate or :autosave is turned on for the association.
# File lib/active_record/autosave_association.rb, line 240 240: def validate_single_association(reflection) 241: if (association = association_instance_get(reflection.name)) && !association.target.nil? 242: association_valid?(reflection, association) 243: end 244: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.