Rails - 验证关联的存在?

发布于 2024-11-01 16:11:14 字数 107 浏览 0 评论 0原文

我有一个模型 A,它与另一个模型 B 具有“has_many”关联。我有一个业务要求,即插入 A 需要至少 1 个与 B 相关的记录。是否有我可以调用的方法来确保这是真的,或者我需要编写自定义验证吗?

I have a model A that has a "has_many" association to another model B. I have a business requirement that an insert into A requires at least 1 associated record to B. Is there a method I can call to make sure this is true, or do I need to write a custom validation?

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

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

发布评论

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

评论(4

╭ゆ眷念 2024-11-08 16:11:14

您可以使用 validates_presence_of http://apidock.com/rails/ ActiveModel/Validations/ClassMethods/validates_presence_of

class A < ActiveRecord::Base
  has_many :bs
  validates_presence_of :bs
end

或只是 validates
http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates

class A < ActiveRecord::Base
  has_many :bs
  validates :bs, :presence => true
end

但是,如果您将 accepts_nested_attributes_for:allow_destroy => 一起使用,则会出现错误。 true嵌套模型和父验证。在本主题中您可以找到解决方案。

You can use validates_presence_of http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of

class A < ActiveRecord::Base
  has_many :bs
  validates_presence_of :bs
end

or just validates
http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates

class A < ActiveRecord::Base
  has_many :bs
  validates :bs, :presence => true
end

But there is a bug with it if you will use accepts_nested_attributes_for with :allow_destroy => true: Nested models and parent validation. In this topic you can find solution.

∞觅青森が 2024-11-08 16:11:14

-------- Rails 4 ------------

简单的验证 存在对我有用

class Profile < ActiveRecord::Base
  belongs_to :user

  validates :user, presence: true
end

class User < ActiveRecord::Base
  has_one :profile
end

这样,Profile。 create 现在将失败。在保存个人资料之前,我必须使用user.create_profile或关联用户。

-------- Rails 4 ------------

Simple validates presence worked for me

class Profile < ActiveRecord::Base
  belongs_to :user

  validates :user, presence: true
end

class User < ActiveRecord::Base
  has_one :profile
end

This way, Profile.create will now fail. I have to use user.create_profile or associate a user before saving a profile.

旧城空念 2024-11-08 16:11:14

如果要确保关联既存在又保证有效,还需要使用

class Transaction < ActiveRecord::Base
  belongs_to :bank

  validates_associated :bank
  validates :bank, presence: true
end

If you want to ensure that the association is both present and guaranteed to be valid, you also need to use

class Transaction < ActiveRecord::Base
  belongs_to :bank

  validates_associated :bank
  validates :bank, presence: true
end
少女七分熟 2024-11-08 16:11:14

您可以验证与 validates_existence_of (这是一个插件)的关联:

示例片段来自此博客条目

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :taggable, :polymorphic => true
  validates_existence_of :tag, :taggable

  belongs_to :user
  validates_existence_of :user, :allow_nil => true
end

或者,您可以使用validates_linked。正如 Faisal 在答案下方的评论中指出的 , validates_linked 通过运行关联的类验证来检查关联的对象是否有效。它检查是否存在。同样重要的是要注意,零关联被认为是有效的。

You can validate associations with validates_existence_of (which is a plugin):

Example snippet from this blog entry:

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :taggable, :polymorphic => true
  validates_existence_of :tag, :taggable

  belongs_to :user
  validates_existence_of :user, :allow_nil => true
end

Alternatively, you can use validates_associated. As Faisal notes in the comments below the answer, validates_associated checks if the associated object is valid by running the associated class validations. It does not check for the presence. It's also important to note that a nil association is considered valid.

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