我有兩個型號:Rails 3 - 驗證2個模型和其他警告?
- 講座
- 招生
本次講座的容量和等候名單。如果有演講報名,我想驗證是否有免費座位。
創建了兩個幫手爲:
def availableSeats
return self.capacity - self.enrollments.confirmedEnrollments.count
end
def waitListAvailable
return self.waitListCapacity - self.enrollments.waitList.count
end
我想過有在招生控制器的檢查,但它不工作。
if(@lecture.availableSeats <= 0)
if(@lecture.waitListAvailable <= 0)
flash[:error] = "Enrolment not possible as the waiting list is full."
# interrupt and don't save, but how?
else
flash[:warning] = "You are on the waiting list."
@enrollment.confirmed = nil
end
else
@enrollment.confirmed = DateTime.now
end
任何想法如何工作?