Hello,
I am trying to deal with a database table that contains a date in the
form of the number of seconds since epoch. Data is inserted in this
format by an existing script that I can’t change, so a schema change
isn’t realistic.
I would like to allow humans to view and edit this data in the form of
an actual human-readable date. I figured a facade column would be the
way to go here, but I am stuck on the fact that the inputs from the
facade type are multiparameter. This breaks the update_attributes call
in ActiveRecord::Base with a NoMethodError in my object’s controller’s
update method.
I have tried to add an additional attribute to the object’s attributes
hash, but this code (which I put in the controller’s update method)
fails silently - the attributes written out are the same before and
after:
puts "the attributes currently are:"
@event.attributes.each do |key, value|
puts "#{key} = #{value}"
end
@event.attributes.store("occurred_at", nil)
puts "after attempting to add an attribute, the attributes are:"
@event.attributes.each do |key, value|
puts "#{key} = #{value}"
end
Any suggestions? Has anyone used a facade column on a Date? Since the
problem occurs in the controller update method, I can’t see how to use
a callback to do the conversion, am I wrong?
(Is part of my problem the name of the column, which is “updated_at” ?
I tried renaming it for testing but got the same error.)
Thanks,
Rachel
Snips from the applicable files:
event.rb
class Event < ActiveRecord::Base
# create a a field based on updated_at, that’s an actual Date kind
# of object
def occurred_at
Time.at( read_attribute(“updated_at”) )
end
def occurred_at=(date)
write_attribute( “updated_at”, date.to_i )
end
end
event_controller.rb
…
def update
@event = Event.find(params[:id])
if @event.update_attributes(params[:event])
flash[:notice] = ‘Event was successfully updated.’
redirect_to :action => ‘show’, :id => @event
else
render :action => ‘edit’
end
end
…
_form.rhtml
…
Occurred at
<%= datetime_select 'event', 'occurred_at' %>
This displays the date in the dropdowns correctly. However, if I try to
save a change (or even the same data), I get this:
NoMethodError in Event#update
You have a nil object when you didn’t expect it!
The error occured while evaluating nil.klass
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1685:in
execute_callstack_for_multiparameter_attributes' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1684:in
each’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1684:in
execute_callstack_for_multiparameter_attributes' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1677:in
assign_multiparameter_attributes’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1339:in
attributes=' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1266:in
update_attributes’
#{RAILS_ROOT}/app/controllers/event_controller.rb:34:in `update’