I don't see a way to alias Rails' auto-timestamp columns...

46 views
Skip to first unread message

ed_ruder

unread,
Sep 12, 2007, 3:13:42 PM9/12/07
to Ruby on Rails meets the Legacy World
I'm working with a legacy database that has columns for creation and
updated times, but they aren't called "created_[at|on]" and
"updated_[at|on]". I haven't found a "proper" way to tell Rails to use
other columns for its automagic timestamping functionality. Am I out
of luck?

Thanks for any help/advice!

Ed

ed_ruder

unread,
Sep 13, 2007, 2:39:45 AM9/13/07
to Ruby on Rails meets the Legacy World
I think I found the solution to my problem here:
http://rails.raaum.org/activerecord.html#alias.

If my legacy DB has a datetime-equivalent column named
"last_modified", I ought to be able to do this in my model:

alias_attribute :updated_at, :last_modified

Ed

ed_ruder

unread,
Sep 13, 2007, 4:51:30 PM9/13/07
to Ruby on Rails meets the Legacy World
Unfortunately, while elegant, alias_attribute() doesn't work as I
expected. It does define getter, setter, and query methods for the
alias, but that's not sufficient.
ActiveRecord::Timestamp.create_with_timestamps and
update_with_timestamps use write_attribute(), which sets values in the
@attributes hash, and doesn't use the setter that alias_attribute set
up.

So, back to square one.

Ed

P.S. The work-around that does work for this is to define a
before_save method in the model that updates the legacy column. I was
hoping for a something that would hook Rails' existing automatic
timestamping functionality. :-[

Reply all
Reply to author
Forward
0 new messages