简体   繁体   中英

adding devise to User model with different table name

I'm working with a pre-existing database where the table for user data is t_customer. To stick with rails convention, I named the associated model "User"

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  self.table_name = "t_customer"
  self.primary_key = "cust_id"
end

Then I ran rails generate devise User, followed by rake db:migrate, but the rake was aborted with the error

UndefinedTable: ERROR: relation "users" does not exist

How do I fix? Is this the wrong approach? Should I have run rake db:migrate or something similar after creating the user model, but before creating the devise migration? I don't know how to get rails to know that :users refers to the t_customer table in the migration file:

class AddDeviseToUsers < ActiveRecord::Migration
  def self.up
    change_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

Just change table_name

class AddDeviseToUsers < ActiveRecord::Migration
  def self.up
    change_table(:t_customer) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM