2
我至少有2個類。一個類必須根據關聯模型的屬性值驗證其屬性之一。下面的代碼是我所要做的,但它只是一個想法,它不起作用。任何方式來實現它?ActiveModel基於關聯驗證屬性
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
只是一個猜測,但是在你的lambda中引用'self'沒有問題嗎?我會去'=> lambda {| record | record.concert.end_date}' – pduersteler
你是對的。該代碼僅僅是爲了說明即將完成的工作,它不是實際的工作代碼。我可以簡單地描述一下我正在嘗試做的事情,我的代碼比單詞更容易。我會嘗試你所說的。 – chris
然後,你應該提供真實的例子,所以我們可以給予真正的幫助;) – pduersteler