How to make a unique column in rails active records other than id
2 answers
The option unique
should be passed to the call add_index
, not where you define your column.
Update the migration definition as follows:
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.string "email"
end
add_index :users, :email, unique: true
end
...
end
And if you don't want to allow null then use t.string :email, null: false
.
+13
source to share