Ruby-on-rails ActiveRecord 何时会保存关联?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18703036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When will ActiveRecord save associations?
提问by hrdwdmrbl
1) I know that it will save associations when autosave: trueas per http://railsapi.com/doc/rails-v2.3.8/classes/ActiveRecord/AutosaveAssociation.html
1)我知道它会autosave: true根据http://railsapi.com/doc/rails-v2.3.8/classes/ActiveRecord/AutosaveAssociation.html保存关联
2) I know that it will save associations that are constructed like
2)我知道它会保存像这样构造的关联
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
3) I think associations are also saved when they are assigned or added
3)我认为关联在分配或添加时也会保存
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 bookto verify what I think is going to be saved will actually be saved.
但是,我有一个复杂的错误,它涉及到一些在我期望的时候不会自动保存的东西。所以,我想通过检查来调试book以验证我认为将要保存的内容实际上会被保存。
TL; DR;
What internal state is checked when saving associations? I'm assuming that a model has an internal instance variable like associations_to_savethat associations get added to when they are created. Then, when the model is saved, it loops through those associations and saves them too.
TL; 博士;保存关联时检查什么内部状态?我假设一个模型有一个内部实例变量,就像associations_to_save在创建关联时添加的那样。然后,当模型被保存时,它会遍历这些关联并保存它们。
采纳答案by BroiSatse
Unfortunately there are no such thing like associations_to_save. However there are some rules saying what is being saved when. You can find those here: http://guides.rubyonrails.org/association_basics.html. Points: 4.1.5 (belongs_to), 4.2.5 (has_one), 4.3.4 (has_many) and 4.4.4 (habtm).
不幸的是,没有像associations_to_save 这样的东西。然而,有一些规则说明什么时候保存。你可以在这里找到这些:http: //guides.rubyonrails.org/association_basics.html。得分:4.1.5 (belongs_to)、4.2.5 (has_one)、4.3.4 (has_many) 和 4.4.4 (habtm)。
UPDATE:
更新:
In case of has_many association, the child is saved on saving the parent if child.new_record? returns true (child was not yet saved to db), or the foreign_key column needs to be updated. This is why:
在 has_many 关联的情况下,如果 child.new_record? 返回 true(子项尚未保存到 db),或者外键列需要更新。这就是为什么:
- Adding object to association on saved parent do save new child.
- Adding object to association on unsaved parent doesn't save (no foreign key value)
- If unsaved parent is being saved and has some child objects in association cache, those objects are saved to update foreign_key.
- 将对象添加到已保存父项上的关联确实会保存新子项。
- 将对象添加到未保存的父项上的关联不会保存(无外键值)
- 如果正在保存未保存的父对象并且在关联缓存中有一些子对象,则保存这些对象以更新外键。
回答by Matt H
Not sure if this will help anyone else, but I recently ran into a similar issue recently in Rails 5.2.
不确定这是否对其他人有帮助,但我最近在 Rails 5.2 中遇到了类似的问题。
When trying to save an object 2 layers deep my tests failed if the top level and the first level objects had already been saved. Ie.
当尝试保存 2 层深的对象时,如果顶级和第一级对象已经保存,我的测试就会失败。IE。
book_cover.persisted? == true
book_cover.book.persisted? == true
page = book_cover.book.pages.new
page.persisted? == false
# After saving the top level object
book_cover.save
page.persisted? == false
# After saving the immediate parent of page
book_cover.book.save
page.persisted? == true
Since the parent "book cover" wasn't the direct parent of the new object "page" saving "book cover" didn't actually end up saving the "page" object.
由于父“书籍封面”不是新对象“页面”的直接父对象,因此保存“书籍封面”实际上并没有最终保存“页面”对象。
Depending on the situation I just explicitly called save on the "book" object to save all the child objects.
根据具体情况,我刚刚在“书”对象上显式调用了 save 来保存所有子对象。

