Today I Learned

hashrocket A Hashrocket project

Generate Rails Model With Unique Attribute

When generating a rails column, you can specify an attribute to have a unique constraint like this:

rails g model Song title:string:uniq

In your migration, this will look like:

add_index :songs, :title, unique: true
See More #rails TILs
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.