Rails 3 的默认值是多少:依赖于 has_many 和 Belongs_to
在rails 3中,我知道我可以使用 :dependent => 强制删除belongs_to和has_many关系上的依赖对象。 : 删除选项。但是我想知道,
如果我不指定 :dependent => ,默认行为是什么? ...
干杯, 哈条
In rails 3, i know that i can force deletion of dependent objects on belongs_to and has_many relations using the :dependent => :delete option. However i was wondering,
what is the default behavior if i do not specify :dependent => ...
Cheers,
Hajo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文档 表示,“如果没有给出任何选项,则行为是在销毁记录时不对关联记录执行任何操作。”也就是说,删除或销毁一个对象不会删除或销毁它所属或拥有多个的对象。
The documentation says, "When no option is given, the behavior is to do nothing with the associated records when destroying a record." That is, deleting or destroying an object will not delete or destroy the objects that it belongs to or has many of.
has_many 使用 :nullify 策略,该策略会将foreign设置为null。对于 has_many :through 它将使用delete_all。
-- ActiveRecord::Associations::ClassMethods
不确定是否属于_to 到底做什么,并且无法在文档中找到任何内容。我会尽快进行一些挖掘并更新答案。
has_many uses the :nullify strategy, which will set the foreign to null. For has_many :through it will use delete_all.
-- ActiveRecord::Associations::ClassMethods
Not sure exactly what belongs_to does, and wasn't able to find anything in the docs. I'll try to do some digging soon and update the answer.
在 Rails 3 中,默认的
:dependent
值为:nullify
,它将外键设置为 nil。资料来源:http://guides.rubyonrails.org/3_1_release_notes.html#active-record
Rails 4 中仍然如此。
来源:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Delete+or+destroy%3F
另外请参阅源代码文档:https://github.com/rails/rails/blob/b5a8fd7bb4a6fa4b67d4eabae4cea2cb1834d8d9/activerecord/lib/active_record/associations/collection_proxy.rb#L369
In Rails 3, the default
:dependent
value is:nullify
which sets foreign keys to nil.Source: http://guides.rubyonrails.org/3_1_release_notes.html#active-record
This is still the case in Rails 4.
Source: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Delete+or+destroy%3F
Also see the source code docs: https://github.com/rails/rails/blob/b5a8fd7bb4a6fa4b67d4eabae4cea2cb1834d8d9/activerecord/lib/active_record/associations/collection_proxy.rb#L369