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?
Is this the "right" way to do this, or should i just fix the value up
in
the controller before calling save?
Thanks,
Denny Reiter
irb(#<User_monitor:0x8dfc470>):007:0> self
=> #<User_monitor:0x8dfc470
@__bp_file="./script/../config/../app/models/user_monitor.rb",
@attributes={"updated_at"=>Wed Oct 26 13:10:50 EDT 2005,
"interface_id"=>"2",
"type"=>"User_monitor",
"monitor_end"=>"365", "monitor_start"=>Wed
Oct 26 00:00:00 EDT 2005, "user_id"=>"1"}, @__bp_line=5,
@new_record=true, @errors=#<ActiveRecord::Errors:0x8dee2d0
@base=#<User_monitor:0x8dfc470 ...>, @errors={}>>
irb(#<User_monitor:0x8dfc470>):008:0> self.monitor_end
=> nil
irb(#<User_monitor:0x8dfc470>):009:0>
--
Denny Reiter
denny-wJvvXGulOwdAfugRpC6u6w@public.gmane.org
It''s not a bug, it''s tradition!
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.
Mark Reginald James wrote:> I''d recommend adding to the class an attr_reader called > monitor_end_num_days,Sorry, that should be an attr_accessor. -- We develop, watch us RoR, in numbers too big to ignore.
On Thu, Oct 27, 2005 at 04:17:37AM +1000, Mark Reginald James wrote:> 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.)That does indeed work. Thank You! I agree with your recommendation, though of using the attr_accessor, it will be much more understandable. Thanks again, Denny Reiter -- Denny Reiter denny-wJvvXGulOwdAfugRpC6u6w@public.gmane.org Yes, I''ve heard of "decaf." What''s your point?