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.