【发布时间】:2014-05-18 04:47:19
【问题描述】:
如果父文档中的属性为真或假,我正在尝试根据条件validate_presence_of 子属性。请在下面查看我的模型:
class Venue
include Mongoid::Document
field :name, type: String
field :has_locations, type: Mongoid::Boolean
embeds_many :locations
accepts_nested_attributes_for :locations
end
还有子(嵌入式)模型。
class Location
include Mongoid::Document
field :city
embedded_in :venue, inverse_of: :locations
# Here I want to validate presence of :city, but only if
# :has_locations in Venue is true
validates_presence_of :city, if: ????
end
然后我有一个带有:has_locations 和fields_for 复选框的表单,用于嵌套位置属性。我的视图和控制器都设置好了,我想我只是不明白如何在模型中进行这种子父条件验证。任何帮助表示赞赏!
【问题讨论】:
标签: ruby-on-rails validation mongoid associations conditional