Fork me on GitHub

Zeke Sikelianos

Mumblings from the code swamp. Find my more interesting online-ness at zeke.sikelianos.com.

Creating Polymorphic Columns in a Rails (2+) Migration

This is a small convenience, but I just discovered it so I thought I’d share. The following migration will add an attachment_id column and a string attachment_type column with a default value of ‘Photo’.

You can also use belongs_to instead of references if that seems more intuitive; It will have the same effect. For more info see the Rails Guide on Migrations or this slightly more in-depth post.


Page 1 of 1