Forum: Ruby on Rails Updating Fields In Migration for HABTM Relationship

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John K. (Guest)
on 2006-06-10 00:31
(Received via mailing list)
Earlier I forgot to add updated_at and created_at columns for my
HABTM relationship so I created a migration to add them.  In the
migration I also want to go back and set the value of the created_at
and updated_at columns for the previous rows.  What is the best way
to do that?

Initially I thought I was being smart and decided to put an UPDATE
SQL query in a find_by_sql statement and then I could not deal with
it's uglyness.  Any much wiser suggestions?

John K.
http://www.kopanas.com


============================================================
http://www.soen.info - Index of online software engineering knowledge
http://www.cusec.net - Canadian University Software Engineering
Conference
http://www.soenlive.com - Presentations from CUSEC
John G. (Guest)
on 2006-06-10 00:40
I don't know whether this is the best way, but it's the only way I know
to do it with migrations. Use an exec statement to do things you can't
do with the migration interface.

exec( 'UPDATE thetable SET updated_at = NOW()' )

John K. wrote:
> Earlier I forgot to add updated_at and created_at columns for my
> HABTM relationship so I created a migration to add them.  In the
> migration I also want to go back and set the value of the created_at
> and updated_at columns for the previous rows.  What is the best way
> to do that?
>
> Initially I thought I was being smart and decided to put an UPDATE
> SQL query in a find_by_sql statement and then I could not deal with
> it's uglyness.  Any much wiser suggestions?
>
> John K.
> http://www.kopanas.com
>
>
> ============================================================
> http://www.soen.info - Index of online software engineering knowledge
> http://www.cusec.net - Canadian University Software Engineering
> Conference
> http://www.soenlive.com - Presentations from CUSEC
This topic is locked and can not be replied to.