嵌套形式 - 如何根据子模型的输入验证父模型?

发布于 2024-09-18 16:14:44 字数 776 浏览 0 评论 0原文

有一个嵌套表单,关系就像这样,

class Inspection < ActiveRecord::Base
  has_many :inspection_components
  accepts_nested_attributes_for :inspection_components

class InspectionComponent < ActiveRecord::Base
  belongs_to :inspection

我在 Inspection 中有一个自定义验证方法,该方法取决于为 InspectionComponent 输入的属性。如何验证 - InspectionComponent 属性未保存或在检查验证中不可用。

谢谢!

编辑:为了让事情更清楚一些,这是我正在尝试做的一个例子。

检查具有属性状态。 InspectionComponent 还有一个属性 status。

Inspection 编辑表单具有嵌套的 InspectionComponents,并且可以更新此表单上的每个模型的状态。仅当所有 @inspection_component.status == 'complete' 时,@inspection.status 才应被标记为 'complete'。

因此,在验证@inspection时,我必须能够看到用户为@inspection_component.status输入的内容。

显然,我可以访问控制器中两个实例的参数,但是在应该进行验证的模型中,我没有看到实现这种情况的方法。

希望这是清楚的,谢谢。

Have a nested form, the relationship is like so

class Inspection < ActiveRecord::Base
  has_many :inspection_components
  accepts_nested_attributes_for :inspection_components

class InspectionComponent < ActiveRecord::Base
  belongs_to :inspection

I have a custom validate method in Inspection which depends on attributes entered for InspectionComponent. How can I validate - InspectionComponent attributes are not saved or available in validation for Inspection.

Thanks!

EDIT: To make things a bit more clear, here's an example of what I'm trying to do.

Inspection has an attribute status.
InspectionComponent also has an attribute status.

The Inspection edit form has nested InspectionComponents and one can update each model's status' on this form. @inspection.status should only be able to be marked 'complete' if all @inspection_component.status == 'complete'.

Therefore, when validating @inspection, I must be able to see what the user entered for @inspection_component.status.

Obviously I have access to the params of both instances in the controller however in the model, where validation should occur, I don't see a way of making this happen.

Hopefully that is clear, thanks.

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

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

发布评论

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

评论(2

只有影子陪我不离不弃 2024-09-25 16:14:44

好的,一个新的答案,以防我发布的另一个答案对其他人有用。针对您的问题,我认为您需要这样的东西:

class Inspection < ActiveRecord::Base
  has_many :inspection_components
  accepts_nested_attributes_for :inspection_components

  # we only care about validating the components being complete
  # if this inspection is complete. Otherwise we don't care.
  validate :all_components_complete, :if => :complete

  def complete
    self.status == 'complete'
  end

  def all_components_complete
    self.inspection_components.each do |component|
      # as soon as you find an incomplete component, this inspection
      # is INVALID!
      Errors.add("field","msg") if component.status != 'complete'
    end
  end
end

class InspectionComponent < ActiveRecord::Base
  belongs_to :inspection
end

Ok, a new answer in case the other one I posted is useful to someone else. Specifically for your issue, I think you need something like this:

class Inspection < ActiveRecord::Base
  has_many :inspection_components
  accepts_nested_attributes_for :inspection_components

  # we only care about validating the components being complete
  # if this inspection is complete. Otherwise we don't care.
  validate :all_components_complete, :if => :complete

  def complete
    self.status == 'complete'
  end

  def all_components_complete
    self.inspection_components.each do |component|
      # as soon as you find an incomplete component, this inspection
      # is INVALID!
      Errors.add("field","msg") if component.status != 'complete'
    end
  end
end

class InspectionComponent < ActiveRecord::Base
  belongs_to :inspection
end
中性美 2024-09-25 16:14:44

您想要使用validates_linked

可能是这样的:

validates_associated :inspection_components

对其进行搜索并查找 api。此方法还有一些有用的选项。

You want to use validates_associated.

probably something like:

validates_associated :inspection_components

Do a search on it and look up the api. There are some useful options for this method as well.

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