David Clements
2006-Feb-05 04:22 UTC
[Rails] Where is a good place to update a date field?
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060205/73be046f/attachment.html
Wilson Bilkovich
2006-Feb-05 05:42 UTC
[Rails] Where is a good place to update a date field?
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?
David Clements
2006-Feb-05 21:37 UTC
[Rails] Where is a good place to update a date field?
On 2/4/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:> > 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?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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060205/28d4f275/attachment.html