2

我至少有2节课。一个类必须根据关联模型的属性值验证其属性之一。下面的代码是我想要的,但这只是一个想法,它不起作用。有什么方法可以实现吗?

class Concert
 include Mongoid::Document
 include Mongoid::Timestamps

 field :end_date, type: Date
end

class Sale
  include Mongoid::Document

  field :end_date, type: Date

  belongs_to :concert

  validates :end_date, :timeliness => {
        :before => lambda {self.concert.end_date}, 
        :after => lambda {self.concert.created_at}, 
        :before_message => 'Sale should not end before the Concert begins', 
        :after_message => 'Sale should not end after the Concert has already ended',
        :type => :date
    }
end
4

2 回答 2

1

向销售添加验证

validates :end_date, :presence => true, :if => :some_checking

def some_checking
   #your validations
   #eg
   self.concert.end_date.present?
end
于 2012-08-29T06:51:17.747 回答
1

self只是一个猜测,但是您在 lambda 中的引用没有问题吗?我会去=> lambda { |record| record.concert.end_date }

于 2012-09-10T20:54:40.237 回答