Rails 3 的默认值是多少:依赖于 has_many 和 Belongs_to

发布于 2024-11-17 12:01:46 字数 152 浏览 3 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄想挽回 2024-11-24 12:01:46

文档 表示,“如果没有给出任何选项,则行为是在销毁记录时不对关联记录执行任何操作。”也就是说,删除或销毁一个对象不会删除或销毁它所属或拥有多个的对象。

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.

栖迟 2024-11-24 12:01:46

has_many 使用 :nullify 策略,该策略会将foreign设置为null。对于 has_many :through 它将使用delete_all。

对于has_many,destroy总是会调用has_many的destroy方法
记录被删除以便运行回调。然而删除将
要么按照指定的策略进行删除
:dependent 选项,或者如果没有给出 :dependent 选项,那么它将
遵循默认策略。默认策略是 :nullify (设置
外键为零),除了 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.

For has_many, destroy will always call the destroy method of the
record(s) being removed so that callbacks are run. However delete will
either do the deletion according to the strategy specified by the
:dependent option, or if no :dependent option is given, then it will
follow the default strategy. The default strategy is :nullify (set the
foreign keys to nil), except for has_many :through, where the default
strategy is delete_all (delete the join records, without running their
callbacks).

-- 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.

初心未许 2024-11-24 12:01:46

在 Rails 3 中,默认的 :dependent 值为 :nullify ,它将外键设置为 nil。

常规 has_many 的默认策略是 :nullify。此外,只有当源反射是属于belongs_to时,这才起作用。

资料来源:http://guides.rubyonrails.org/3_1_release_notes.html#active-record

Rails 4 中仍然如此。

但是,deletedelete_all 将根据 :dependent 选项指定的策略进行删除,或者如果没有 给定 :dependent 选项,则遵循默认策略。默认策略为 :nullify(将外键设置为 nil),has_many :through 除外,其中默认策略为 delete_all(删除连接记录,而不运行其回调)。

来源: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.

The default strategy is :nullify for regular has_many. Also, this only works at all if the source reflection is a belongs_to.

Source: http://guides.rubyonrails.org/3_1_release_notes.html#active-record

This is still the case in Rails 4.

However delete and delete_all will either do the deletion according to the strategy specified by the :dependent option, or if no :dependent option is given, then it will follow the default strategy. The default strategy is :nullify (set the foreign keys to nil), except for has_many :through, where the default strategy is delete_all (delete the join records, without running their callbacks).

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文