Forum: Ruby on Rails Where is a good place to update a date field?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bd6167863690cb320d65bb9204619817?d=identicon&s=25 David Clements (Guest)
on 2006-02-05 05:25
(Received via mailing list)
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?

Any thoughts?

Thanks,

Dave
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-02-05 06:43
(Received via mailing list)
On 2/4/06, David Clements <digidigo@gmail.com> wrote:
>
>  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?
Bd6167863690cb320d65bb9204619817?d=identicon&s=25 David Clements (Guest)
on 2006-02-05 22:40
(Received via mailing list)
On 2/4/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
>
> 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.

Thanks for the response,

Dave
This topic is locked and can not be replied to.