I know that it will save associations when
autosave: true
as per https://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.htmlI know that it will save associations that are constructed like
book = Book.new(name: 'foo')
book.authors.build(name: 'bar') #has_many
book.save
or like
book = Book.new(name: 'foo')
book.build_author(name: 'bar') #has_one
book.save
- I think associations are also saved when they are assigned or added
book = Book.new(name: 'foo')
book.author = Author.new(name: 'bar')
book.save
or
book = Book.new(name: 'foo')
book.authors << Author.new(name: 'bar')
book.save
But, I have a complicated bug that involves something not auto-saving when I would expect it to. So, I want to debug by inspecting book
to verify what I think is going to be saved will actually be saved.
TL; DR;
What internal state is checked when saving associations? I'm assuming that a model has an internal instance variable like associations_to_save
that associations get added to when they are created. Then, when the model is saved, it loops through those associations and saves them too.
3.2.13
. Can you be more specific about what didn't work properly in earlier versions? – Ollie