简体   繁体   English

使用不同的表名将设计添加到用户模型

[英]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. 我正在使用一个现有数据库,其中用户数据表为t_customer。 To stick with rails convention, I named the associated model "User" 为了遵守Rails约定,我将关联的模型命名为“ 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 然后我运行rails generate devise User,然后运行rake db:migrate,但是rake因错误而中止

UndefinedTable: ERROR: relation "users" does not exist UndefinedTable:错误:关系“用户”不存在

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? 在创建用户模型之后,但在创建设计迁移之前,我应该运行rake db:migrate或类似工具吗? I don't know how to get rails to know that :users refers to the t_customer table in the migration file: 我不知道如何使:users引用迁移文件中的t_customer表:

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 只需更改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: ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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