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.
3b985461018c5edb36619bc30a70548d?d=identicon&s=25 John Kopanas (Guest)
on 2006-06-09 22: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 Kopanas
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
3c43c3dbb6d107d7b382cee90ca39817?d=identicon&s=25 John Gray (brainpipe)
on 2006-06-09 22: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 Kopanas 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 Kopanas
> 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.