Links
Twitter
Flickr
« Getting paid | Main | Kick ass build and continuous integration blog »
Thursday
May152008

The principle of least suprise and rails migrations



tim@localhost:~/Development/getfree$ script/generate migration \
add_polymorphic_foreign_key_to_attachments resource_id:integer resource_type:string
exists db/migrate
create db/migrate/20080515094515_add_polymorphic_foreign_key_to_attachments.rb

tim@localhost:~/Development/getfree$ rake db:migrate
(in /Users/tim/Development/getfree)
== 20080515094515 AddPolymorphicForeignKeyToAttachments: migrating ============
-- add_column(:attachments, :resource_id, :integer)
-> 0.0422s
-- add_column(:attachments, :resource_type, :string)
-> 0.0150s
== 20080515094515 AddPolymorphicForeignKeyToAttachments: migrated (0.0731s) ===



So, that's pretty cool. I wasn't thinking very hard when I wrote this generate migration command but Rails just figured out that i must want to add these new fields to the attachments table based on the way I named the migration.

I didn't know rails did this when I used it this way, in fact after I ran in the migration I was surprised it worked because I thought I'd forgotten to go in and tell it which table to add this fields to...

Actually, it's not just pretty cool, that's very cool.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>