I have a field that indicate that a reservation has been cancelled. I
want
to update a cancellation_date field with the date that the reservation
is
cancelled. I am trying to think of where the best place to do that is?
pre-requisites or permissions to your Reservation model. Just call @reservation.cancel from some controller action, after the user tells
you they want to cancel.
Or, better, something like: @reservation.cancel unless @reservation.cancelled?
Yeah, I should have been more clear.
The reservation has a status that can be an number of things and within
that
set there is a number of “cancelled” states. So when the status changes
to
one of the cancelled states I need to set the cancellation date field as
well.
I ended up using the before_update hook for this. I guess that is a
reasonable place, but I would say that it is not clear the best place
for
setting related fields. The other option would be to create a trigger,
but
it is so easy to just do it with a hook.
I have a field that indicate that a reservation has been cancelled. I want
to update a cancellation_date field with the date that the reservation is
cancelled. I am trying to think of where the best place to do that is?
I like this kind of thing:
class Reservation < ActiveRecord::Base
def cancelled?
!self[:cancellation_date].nil?
end
def cancel
self[:cancellation_date] = Date.today
end
end
This gives you a single place to make changes if you add
pre-requisites or permissions to your Reservation model. Just call @reservation.cancel from some controller action, after the user tells
you they want to cancel.
Or, better, something like: @reservation.cancel unless @reservation.cancelled?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.