Add new fields in migration?

1

Hello I'm working with Ruby On Rails, and for platform authentication I'm using gem 'devise'. I came across a situation where I need to add new fields for user registration (Date of birthday, gender, etc ...) and the device only provides me the basic resources (email, password). To add these fields do I need to simply put them inside the migration or do I need to do some extra steps?

Part of the code generated by devise:

def change create_table :members do |t| ## Database authenticatable t.string :email, null: false, default: '' t.string :encrypted_password, null: false, default: ''

To add new fields just do this?

def change create_table :members do |t| ## Database authenticatable t.string :email, null: false, default: '' t.string :encrypted_password, null: false, default: '' t.string :MEU_NOVO_CAMPO, null: true, default: ''

Thank you.

    
asked by anonymous 25.05.2017 / 15:03

2 answers

1

In addition to generating a migration with the new fields

rails g migration AddNewFieldsToMembers

What will create the migrate

class AddNewFieldsToMembers < ActiveRecord::Migration
  def change
    add_column :members, :field_name, :type
  end
end

You will still have to create the devise controller

rails g devise:controller user

It will create a folder with user controllers. In these controllers you will have to add the fields in some methods to give access permission.

More information link search about custom fields.

    
25.05.2017 / 17:33
0

Yes, the path is to generate another migration that will add the new fields. Something like:

rails g migration AddNewFieldsToMembers

class AddNewFieldsToMembers < ActiveRecord::Migration
  def change
    add_column :members, :field_name, :type
  end
end

Here you can see the list of available types for Rails version and specific database:

    
25.05.2017 / 16:09