开发者

Ruby on Rails + devise: How can i create customize users table with devis rake db:migrate?

rails generate devise User I got this=>

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :u开发者_运维知识库sers, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

But i want to create a users table with username, email, password, role, group, mark, created_at, modified_at columns.

How can i do this ?

Is this structure correct to have username, password, email, group, role, mark ?

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable

      t.string :username
      t.string :password
      t.string :email
      t.string :group
      t.string :role
      t.integer :mark

      t.timestamps
  end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

What are these?

t.database_authenticatable :null => false
          t.recoverable
          t.rememberable
          t.trackable


You can perform a migration to add some fields to the user table. For example:

rails g add_fields_to_users username:string # as well as other fields you need

Then, in order to add columns to your table run:

rake db:migrate

Devise has already generated some columns you need like: email, password, created_at, updated_at...

For adding roles to your user model you should watch the cancan screencast: railscasts and also read the doc in order to see some updates.

EDIT:

If you want to add fields manually you could add them in your self.up method before running your migration:

def self.up
  create_table(:users) do |t|

    #...

    t.rememberable
    t.trackable

    t.string :username
    #... your other attributes here

  end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜