根据父模型验证模型

发布于 2024-11-01 10:13:43 字数 325 浏览 1 评论 0原文

我有两个模型,一个是另一个的父模型,父模型accept_nested_attributes_for 和validates_linked 子模型。

但是,我的一些验证有一个 :if ,需要检查父级的属性之一。

我在想我可以做这样的事情:

validates_presence_of :blah, :if => Proc.new{|thing| thing.parent.some_value.present?}

但是,在验证时似乎没有设置“父”关系(我假设孩子首先被实例化和验证。

因此有什么方法可以做我在想可以吗?

I have two models, one is the parent of the other, and the parent accepts_nested_attributes_for and validates_associated the children.

However, some of my validations have an :if that needs to check one of the properties of the parent.

I was thinking that I could do something like this:

validates_presence_of :blah, :if => Proc.new{|thing| thing.parent.some_value.present?}

However, the 'parent' relationship doesn't appear to be setup at the time of validation (I would assume the children get instantiated and validated first.

Therefore is there any way of doing what I'm thinking of? Is it possible?

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

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

发布评论

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

评论(3

昵称有卵用 2024-11-08 10:13:43

您可以根据需要使用 before_update 或 before_create 回调,如下所示。

def before_update
  self.errors.add("Error Message") if self.parent.some_value.present?
  return false if self.errors.count > 0
end

def before_create
  self.errors.add("Error Message") if self.parent.some_value.present?
  return false if self.errors.count > 0
end

You can use before_update or before_create callbacks as per your need like this..

def before_update
  self.errors.add("Error Message") if self.parent.some_value.present?
  return false if self.errors.count > 0
end

def before_create
  self.errors.add("Error Message") if self.parent.some_value.present?
  return false if self.errors.count > 0
end
把昨日还给我 2024-11-08 10:13:43

这种验证应该有效:

验证关联:孩子

但它不会

据我了解,原因是,因为使用acceptes_nested_attributes_for 是通过一个事务直接创建嵌套对象到数据库,而不通过任何子验证。

您可以在这里做什么:在父模型中编写自己的验证并验证创建子对象。

This kind of validation should work:

validates_associated :children

But it won't

The reason is, as far as I understand, beause using acceptes_nested_attributes_for is creating nested objects straight to database via one transaction without passing any children validations.

What you can do here: write your own validation in parent model and validate creating children objects.

梦醒时光 2024-11-08 10:13:43

使用 :inverse_of 选项进行父级关联,因此子级在构建时将拥有对父级的引用。

class Parent < ActiveRecord::Base
  has_many :children, :inverse_of => :parent
  accepts_nested_attributes_for :children
end

class Child < ActiveRecord::Base
  belongs_to :parent
end

p = Parent.new :children_attributes => { 0 => { :child_attribute => 'value' } }
p.children.first.parent #=> shouldn't be nil anymore

Use the :inverse_of option for the association on the parent, so the children will have a reference to the parent when they are built.

class Parent < ActiveRecord::Base
  has_many :children, :inverse_of => :parent
  accepts_nested_attributes_for :children
end

class Child < ActiveRecord::Base
  belongs_to :parent
end

p = Parent.new :children_attributes => { 0 => { :child_attribute => 'value' } }
p.children.first.parent #=> shouldn't be nil anymore
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文