# Newb question about facade columns

**URL:** <https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969>\
**Category:** Rails\
**Created:** [March 2, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969 "2006-03-02T22:11:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rachel\_McConnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@Rachel\_McConnell](https://www.ruby-forum.com/u/Rachel_McConnell)\
**Post date:** [March 2, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969/1 "2006-03-02T22:11:14Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![Rachel\_McConnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@Rachel\_McConnell](https://www.ruby-forum.com/u/Rachel_McConnell)\
**Post date:** [March 3, 2006, 12:21am UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969/2 "2006-03-03T00:21:05Z")

</div>

Rachel McConnell \<rachel@…\> writes:

> 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.

[snip]

> (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.)

updated\_at is automatically updated for all ActiveRecord::Base  
descendants. So  
if this column records something else, you should definitely name it  
something  
else.

Perhaps what you want to do is grab the values from the form in your  
controller  
and then have some logic in the model save it/convert it to the database  
column  
in the proper format.

like add an attribute to your model (but not your db) called “my\_date”  
and have  
an attribute in your database called “epoch\_thingy” or whatever you call  
it:

class MyModel  
attr :my\_date

before\_save :do\_that\_date\_thing

def do\_that\_date\_thing  
#do some kind of crazy date conversion here  
self.epoch\_thingy = my\_conversion(self.my\_date)  
end  
end

Something like that…?

-damon

> **[Damon Clinkscales](https://damonc.onuniverse.com/)**
>
> Damon Clinkscales

---

<div class="post-metadata">

**Author:** ![Rachel\_McConnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@Rachel\_McConnell](https://www.ruby-forum.com/u/Rachel_McConnell)\
**Post date:** [March 3, 2006, 12:51am UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969/3 "2006-03-03T00:51:19Z")

</div>

Rachel McConnell wrote:

> update method.  
> This may work:

## \_form.html

select\_datetime @event.occurred\_at, :prefix =\> ‘event[occurred\_at]’

## event.rb

class Event \< ActiveRecord::Base  
def occurred\_at  
Time.at( updated\_at )  
end

def occurred\_at= (d)  
d.each\_pair { |k,v| d[k] = v.to\_i }  
self.updated\_at =  
Time.local(d[‘year’],d[‘month’],d[‘day’],d[‘hour’],d[‘minute’]).to\_i  
end  
end

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

---

<div class="post-metadata">

**Author:** ![Rachel\_McConnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@Rachel\_McConnell](https://www.ruby-forum.com/u/Rachel_McConnell)\
**Post date:** [March 3, 2006, 3:25am UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969/4 "2006-03-03T03:25:06Z")

</div>

Mark Reginald J. wrote:

> > facade type are multiparameter. This breaks the update\_attributes
> 
> ```
> d.each_pair { |k,v| d[k] = v.to_i }
> self.updated_at = 
> 
> ```
> 
> Time.local(d[‘year’],d[‘month’],d[‘day’],d[‘hour’],d[‘minute’]).to\_i  
> end  
> end

I did think along those lines (and if that should work my problem is  
elsewhere, but i don’t know where). My code actually fails _before_ it  
gets to call the occurred\_at= method of the model object; or maybe that  
would never get called. The controller is using  
@event.update\_attributes which does… something different. The stack  
trace shows this:

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’

Is there a different way to set the input parameters on the model, other  
than

@event.update\_attributes(params[:event])

?

I mean, I could parse the params and attempt to set everything by hand  
and that would probably work, but it kinda defeats the point of Rails’  
infrastructure work.

Rachel

---

<div class="post-metadata">

**Author:** ![Rachel\_McConnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@Rachel\_McConnell](https://www.ruby-forum.com/u/Rachel_McConnell)\
**Post date:** [March 3, 2006, 3:28am UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969/5 "2006-03-03T03:28:08Z")

</div>

Damon C. wrote:

> > (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.)
> 
> updated\_at is automatically updated for all ActiveRecord::Base descendants. So  
> if this column records something else, you should definitely name it something  
> else.

MySQL allows auto-updated columns to be set explicitly; besides, I tried  
renaming it just to see what would happen and that didn’t help. For  
non-coding reasons a schema change really isn’t do-able for me.

> Perhaps what you want to do is grab the values from the form in your controller  
> and then have some logic in the model save it/convert it to the database column  
> in the proper format.

As I understand it, that’s exactly what a column facade does. But (I  
believe) since I’m trying to convert from a multipiece-data format to a  
single-piece format, it isn’t working ☹

Rachel

---

<div class="post-metadata">

**Author:** ![Rachel\_McConnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@Rachel\_McConnell](https://www.ruby-forum.com/u/Rachel_McConnell)\
**Post date:** [March 3, 2006, 3:34am UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969/6 "2006-03-03T03:34:09Z")

</div>

Rachel McConnell wrote:

> @event.update\_attributes which does… something different. The stack  
> trace shows this:
> 
> c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:1685:in  
> `execute\_callstack\_for\_multiparameter\_attributes’

Note that my suggestion uses select\_datetime rather than  
datetime\_select.  
This will prevent the multiparameter attribute assignment from trying to  
occur.

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

---

<div class="post-metadata">

**Author:** ![Rachel\_McConnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@Rachel\_McConnell](https://www.ruby-forum.com/u/Rachel_McConnell)\
**Post date:** [March 3, 2006, 4:37am UTC](https://www.ruby-forum.com/t/newb-question-about-facade-columns/75969/7 "2006-03-03T04:37:21Z")

</div>

Mark Reginald J. wrote:

> Note that my suggestion uses select\_datetime rather than datetime\_select.  
> This will prevent the multiparameter attribute assignment from trying to  
> occur.

You’re right, I missed that! And your code works perfectly. Looks like  
it uses a 2 dimensional array instead of… a Hash, I think - whatever  
datetime\_select produces. Must look into that more.

Thank you very much!

Rachel
