获取子模型中的父值
我有一个名为 RsvpRegistrations 的模型,
belongs_to :rsvp
我需要在验证中使用父“rsvp”对象中的值,例如
validates_presence_of :phone if self.rsvp.phone
(Rsvp.phone 是布尔值)
但这不起作用。我得到的错误是未定义的方法“rsvp”。如何访问父对象及其值?
一旦我让它工作,我就会运行其他类似的验证,所以我想我需要一次获取父级“rsvp”,然后在其他验证中引用它。
提前致谢。
I have a model called RsvpRegistrations with
belongs_to :rsvp
I need to use values from the parent 'rsvp' object in my validations such as
validates_presence_of :phone if self.rsvp.phone
(Rsvp.phone is boolean)
But this doesn't work. The error I get is undefined method `rsvp'. How can I access the parent object and its values?
Once I get it working, I have other similar validations to run, so I'm thinking I need to grab the parent 'rsvp' one time and then reference it in my other validations.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更多选项此处
More options here
如果您有多个验证都引用 RSVP,则创建自定义验证方法可能会更有效:
If you have multiple validations that all reference RSVP, it may be more efficient to create a custom validation method: