何时使用“validates_related"v"验证=>真" [英] When to use "validates_associated" v "validate => true"
问题描述
似乎在 rails 中,您可以在两个地方定义关联验证,或者在关联本身上:
It seems that in rails you can define association validations in two places, either on the association itself:
class Child
belongs_to :parent, :validate => true
end
或作为验证回调:
class Child
belongs_to :parent
validates_associated :parent
end
这两种方法有什么区别?
What is the difference between these two methods?
测试差异
我认为也许前者会产生背压并强制父级仅在子级有效时才有效:
I thought that maybe the former creates a backpressure and enforces that the parent is only valid if the child is valid:
即(当设置 :validate => true 时)
i.e. (when setting :validate => true)
child.valid? # => false
child.parent.valid? # => also evaluates to false because of the :validate => true condition
# do whatever it takes to make the child valid again
#...
child.valid? # => true
child.parent.valid? # => true
但是我测试了它并且这不会发生.那么这两种方法有什么区别(如果有的话)?
However I tested it and this doesn't happen. So what's the difference (if any) between the two methods?
推荐答案
我不得不深入研究 Rails (3.0.7) 代码以找到一些差异.核心功能在我看来是一样的——它们似乎都在关联的记录上调用 valid?
.
I had to dig into the Rails (3.0.7) code to find some differences. The core functionality looks the same to me -- they both seem to call valid?
on the associated record(s).
我发现的主要差异仅在使用 :autosave
功能或销毁关联对象或将其标记为销毁时出现.例如,我有:
The key differences that I did find only appear when using the :autosave
feature or when either destroying the associated object or marking it for destruction. For example, I have:
class AbsentDate < ActiveRecord::Base
belongs_to :user, :autosave => true, :validate => true
end
我看到以下行为:
user = User.new(:username => "Jimmy")
user.valid? # => true
ad = AbsentDate.new(:user => user)
user.username = nil
user.valid? # => false
ad.valid? # => false
ad.errors.full_messages # => ["User username cannot be empty"]
ad.user.mark_for_destruction
ad.valid? # => true
请注意,将用户标记为销毁会导致有效的 AbsentDate.另请注意,只有一条错误消息.现在考虑这种情况:
Note that marking the user for destruction resulted in a valid AbsentDate. Also note that there is only one error message. Now consider this case:
class AbsentDate < ActiveRecord::Base
belongs_to :user, :autosave => true
validates_associated :user
end
这是我所看到的:
user = User.new(:username => "Jimmy")
user.valid? # => true
ad = AbsentDate.new(:user => user)
user.username = nil
user.valid? # => false
ad.valid? # => false
ad.errors.full_messages # => ["User username cannot be empty", "User is invalid"]
ad.user.mark_for_destruction
ad.valid? # => false
这次有两条错误消息,即使其用户已被标记为销毁,AbsentDate 仍然是假的.我能够通过调用 destroy 而不是 mark_for_destruction 来复制这些结果.
Two error messages this time, and the AbsentDate is still false even though its user has been marked for destruction. I was able to replicate these results by calling destroy instead of mark_for_destruction.
最后一件事:如果你使用 validates_related
,你会得到几个选项(:if
、:unless
、:on
, :message
) 如果您在 belongs_to
上使用标志,您将不会拥有.
One last thing: if you use validates_associated
, you get several options (:if
, :unless
, :on
, :message
) that you won't have if you use the flag on belongs_to
.
这篇关于何时使用“validates_related"v"验证=>真"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!