rails g model User name:index email:uniq token:string{6} age:integer
name:indexcreates an indexed column calledname.email:uniqcreates column calledemailwith a unique index.- Specifying
token:string{6}constrains the length oftokento 6 chars.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :token, :limit => 6
t.age :integer
t.timestamps
end
add_index :users, :name
add_index :users, :email, :unique => true
end
end
rails g model Item name user:references
- Columns default to type string by default.
- Foreign keys are added (with indices) by specifying
references.
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :name
t.integer :user_id
t.timestamps
end
add_index :items, :user_id
end
end
Source: 10 Things You Didn't Know Rails Could do by @jeg2.