在我的Rails 4应用程序中,我正在将我的逻辑转移到我的模型中 .
我的模型中的一种方法更改了预订的状态:
def withdraw
if self.status == 1 #only allow bookings with status 1 to be updated
self.status = 2
GuestMailer.booking_withdrawn(self).deliver
save!
end
end
我这样调用我的 BookingsController 中的方法:
if @booking.withdraw
flash[:success] = 'The booking has been withdrawn'
end
我的问题是我应该在模型中使用 save!
,因为我只是在更新?
1 回答
您可以在此方法中使用
save!
. 但是,可以将该方法重命名为withdraw!
,以显示此方法更改对象的状态,并在无法保存模型时引发异常 .