Add new fields in Migration ?

Asked

Viewed 904 times

1

Hello I am working with Ruby On Rails, and for platform authentication I am using Gem 'Devise'. I came across a situation where I need to add new fields for user registration (Birthday date, gender, etc...) and the device only provides me with basic features (email, password). To add these fields I need just put inside the Migration or I need to do some more steps ?

Part of the code generated by the Visa:

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.

2 answers

1

In addition to generating an Migration with the new fields

rails g migration AddNewFieldsToMembers

That will create the migrate

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

You’ll still have to create the Devise controllers

rails g devise:controller user

Which 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 https://github.com/plataformatec/devise look up on custom Fields.

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 types available for Rails version and specific database: https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.