validates_linked 不检查关联的存在

发布于 2024-10-20 04:54:34 字数 971 浏览 0 评论 0原文

谁能弄清楚这里发生了什么事吗?我能够让我的代码按照我想要的方式工作,但我无法弄清楚为什么 validates_linked 没有按我的预期工作。这是我的代码片段:

class Flag < ActiveRecord::Base
  belongs_to :user
  belongs_to :post

  # allow only one flag per post per user
  validates_uniqueness_of :user_id, :scope => :post_id

  validates :user_id, :post_id, :presence => true
  validates_associated :user, :post

  attr_accessible :user_id, :post_id
end

使用此代码,我无法保存 user_id == nil 的标志。我可以保存 user_id == 12345 的标志(即某些 user_id 不在数据库中)。 validates_linked API 规范是这样说的:

validates_linked(*attr_names)

验证关联的一个或多个对象本身是否都有效。与任何类型的协会合作。
...
注意:如果尚未分配关联,则此验证不会失败。如果要确保关联既存在又保证有效,还需要使用 validates_presence_of。

相反,我能够通过使用它来获得所需的行为:

  validates :user, :post, :presence => true

我对 API 规范的理解是,validates_linked 检查关联表以查看是否存在 id 与 Flag 的外键匹配的行,前提是外键是非零。谁能对此提供任何见解?我是否误解了 validates_linked 应该如何工作?

Can anyone figure out what's going on here? I was able to get my code to work the way I want it to, but I can't figure out why validates_associated isn't working as I expect. Here's a snippet of my code:

class Flag < ActiveRecord::Base
  belongs_to :user
  belongs_to :post

  # allow only one flag per post per user
  validates_uniqueness_of :user_id, :scope => :post_id

  validates :user_id, :post_id, :presence => true
  validates_associated :user, :post

  attr_accessible :user_id, :post_id
end

With this code I can't save a flag with user_id == nil. I can save a flag with user_id == 12345 (i.e. some user_id not in the database). This is what the validates_associated API specification says:

validates_associated(*attr_names)

Validates whether the associated object or objects are all valid themselves. Works with any kind of association.
...
NOTE: This validation will not fail if the association hasn’t been assigned. If you want to ensure that the association is both present and guaranteed to be valid, you also need to use validates_presence_of.

I was able to get the desired behavior by using this, instead:

  validates :user, :post, :presence => true

My understanding of the API specification is that validates_associated checks the associated table to see if a row exists with an id matching the foreign key of Flag provided the foreign key is non-nil. Can anyone offer any insight on this? Am I misunderstanding how validates_associated is supposed to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

秋心╮凉 2024-10-27 04:54:34

validates_linked 只是运行关联对象的类中指定的验证,它对外键不执行任何操作。

validates :user_id, :presence=>true 确保标记记录中存在 user_id,但仅此而已。

validates :user, :presence=>true 用于关联本身,并确保正确设置外键。

validates_associated simply runs the validations that are specified within the associated object's class, it does nothing in regard to foreign keys.

validates :user_id, :presence=>true ensures the presence of a user_id in your flag record, but that's all.

validates :user, :presence=>true is used on the association itself and ensures that foreign keys are properly set up.

后来的我们 2024-10-27 04:54:34

伙计...我得到的只是需要 validates_presence_of 才能像您从 API 中获得的那样工作。检查关联有效性似乎有点矫枉过正,但我​​是个菜鸟。

Man... all I got was that validates_presence_of is needed for this to work as you got from the API. Seem's overkill to be checking for association validness, but I'm a noob.

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