我有一個模型Booking
即has_many: appointments
。當創建Booking
時,我也創建了一些Appointments
。after_initialize和before_create之間回調
Appointments
有一個屬性date
需要在創建它之後進行填充。例如。 before_save :set_date
。
然後,我需要在Booking
上進行驗證,以檢查約會是否順序,這需要每個約會的日期。
- 新
Booking
初始化 - 新
Appointments
初始化 Appointments
有其date
屬性設置- 驗證
Booking
。
問題是,當驗證發生時,日期尚未設置。這很有道理,因爲date
在Appointment
上設置爲before_save
,並且保存尚未發生。因此我將before_save
更改爲after_initialise
。在這種情況下,需要填入日期的Appointment
的其他屬性(特別是Block
的外鍵)尚未分配給約會。
我想要在after_initialise
之間設置外鍵,但在before_save
之前的回調。那可能嗎?