0

I'm trying to skip validations of a belongs_to association if a criteria is true. Below is my code

class Venue < ActiveRecord::Base
  has_many :events
  validates_presence_of :name, :postcode, :category
end

class Event < ActiveRecord::Base
  belongs_to :venue
  accepts_nested_attributes_for :venue
end

So what I need to do is skip the Venues validates_presence_of validation if a criteria from the Events model is true. So lets say if the event_type was equal to '1' then it will ignore the Venues validates_presence_of call but if event_type was '2' then it will still execute the validates_presence_of call.

4

2 回答 2

0

关于这个主题有一个Railscast 。您还可以查看Rails 条件验证

根据上面的链接,您必须传递一个 lambda,例如:

:if => lambda { |venue| venue.event.try(:event_type) == 2 }
于 2013-08-29T10:53:07.367 回答
0

最后我做了一些与此非常相似的事情

于 2013-09-09T08:57:01.423 回答