On Oct 1, 7:19 pm, SNelson <[email protected]> wrote:

Well there is some funkiness todo with how time parameters are passed
around, but that is not the problem here. Equally the components of a
date or time object are readonly properties. What I think the core
misconception here is that params is a controller method therefore you
cannot call it from a model instance method. Usually you don't need
to: why fiddle with params[:starts_at] when you already have
self.starts_at ? I'm not sure a callback is right for you here, are
you always going to want to update ends_at even on subsequent saves ?
You could instead have a method that takes a number of hours and
minutes and sets tends at correspondlingly, call that from your
controller. Lastly be careful about your calculations - not sureyour
examples wouldn't work if starts_at hour was 23 and duration_hours was
3 for example)

Fred

>
> class Event < ActiveRecord::Base
>   before_save :create_end
>         def create_end
>                 self.ends_at.year = params[:starts_at[:1i]]
>                 self.ends_at.month = params[:starts_at[:2i]]
>                 self.ends_at.day = params[:starts_at[:3i]]
>                 self.ends_at.hour = params[:starts_at[:4i]] + params
> [:duration_hours]
>                 self.ends_at.min = params[:starts_at[:5i]] + params
> [:duration_minutes]
>         end
> end
>
> That was a mess, so, following a helpful tip, I tried
>
> def create_end
>   ends_at = ends_at.change(:year => starts_at.year, :month =>
> starts_at.month,
>  :day => starts_at.day, :hour => (starts_at.hour + params
> [:duration_hours]),
>  :min => (starts_at.min + params[:duration_minutes]))
> end
>
> I'm clearly botching the syntax for the callback because nothing I
> have tried has worked.
>
> Help?
> Thanks.
>
> Steve
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to