Re: question about before_create

Mark Reginald James
Wed, 26 Oct 2005 18:17:37 -0000

Denny Reiter wrote:
> I''m trying to "fix" a record before it is inserted into
a table.
> I have two attributes, monitor_start and monitor_end.  On some
> forms, instead of the user specifying monitor_end, I want to let
> them specify the length of the monitoring in days.  I''m putting
> this value into monitor_end and then want to fix it using before_create.
> My problem is when I try and access monitor_end it is nil instead
> of the value.  If I insert a breakpoint and look at the object, the
> value is there but I can''t seem to access it.  Is there a way for
> me to pull this value out?

You got nil monitor_end because you''re trying to assign a day count
to an attribute of class Date.

I''d recommend adding to the class an attr_reader called
monitor_end_num_days,
then using this both as the method parameter in the appropriate form helpers
and to set the monitor_end attribute value in before_create.

Or this may work:
monitor_end = monitor_start + monitor_end_before_type_cast.to_i
(Let me know if it does.)

--
We develop, watch us RoR, in numbers too big to ignore.