Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Generate a migration with polymorphic association

The migration generator has an option to create a polymorphic reference:

rails g migration add_taggable_to_tags taggable:references{polymorphic}

It will generate the following migration:

def change
  add_reference :tags, :taggable, polymorphic: true, index: true
end
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.