我有类似的东西:
class Event
validates :name, :prescence => true
belongs_to :parent, class => "Event", foreign_key => "parent_id"
has_many :children, class => "Event"
我想更改它,以便仅当事件没有父级时才需要名称。
最佳答案
validates :name, :presence => true, :if => Proc.new {|event| event.parent.blank? }
应该可以正常工作。请阅读docs欲了解更多信息。
关于ruby-on-rails - Rails 互斥字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447392/