Ruby-on-rails Rails - 验证关联的存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5689888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails - Validate Presence Of Association?
提问by skaz
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?
我有一个模型 A,它与另一个模型 B 有一个“has_many”关联。我有一个业务要求,即插入 A 需要至少有 1 个与 B 相关联的记录。有没有我可以调用的方法来确保这是真的,还是我需要编写自定义验证?
回答by fl00r
You can use validates_presence_ofhttp://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of
您可以使用validates_presence_ofhttp://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of
class A < ActiveRecord::Base
has_many :bs
validates_presence_of :bs
end
or just validateshttp://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates
或者只是validateshttp://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_forwith :allow_destroy => true: Nested models and parent validation. In this topic you can find solution.
但是有一个bug吧,如果你会使用accepts_nested_attributes_for同:allow_destroy => true:嵌套模型和家长确认。在本主题中,您可以找到解决方案。
回答by hexinpeter
-------- Rails 4 ------------
-------- 导轨 4 ------------
Simple validatespresenceworked for me
简单validatespresence对我有用
class Profile < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
end
class User < ActiveRecord::Base
has_one :profile
end
This way, Profile.createwill now fail. I have to use user.create_profileor associate a user before saving a profile.
这样,Profile.create现在将失败。我必须使用user.create_profile或用户节省了前相关联profile。
回答by Spyros
You can validate associations with validates_existence_of(which is a plugin):
您可以验证与validates_existence_of(这是一个插件)的关联:
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 Faisalnotes in the commentsbelow the answer, validates_associatedchecks if the associated object is valid by running the associated class validations. It does notcheck for the presence. It's also important to note that a nil association is considered valid.
或者,您可以使用validates_associated. 正如Faisal在答案下方的评论中指出的那样,validates_associated通过运行关联的类验证来检查关联的对象是否有效。它不检查存在。同样重要的是要注意 nil 关联被认为是有效的。
回答by Sukeerthi Adiga
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

