【问题标题】:adding devise to User model with different table name将设计添加到具有不同表名的用户模型
【发布时间】:2015-03-24 12:32:33
【问题描述】:

我正在使用一个预先存在的数据库,其中用户数据的表是 t_customer。为了遵守 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

然后我运行 rails generate devise User,然后运行 ​​rake db:migrate,但 rake 因错误而中止

UndefinedTable:错误:关系“用户”不存在

我该如何解决?这是错误的方法吗?在创建用户模型之后,但在创建设计迁移之前,我是否应该运行 rake db:migrate 或类似的东西?我不知道如何让 rails 知道 :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: ""

【问题讨论】:

    标签: ruby-on-rails database devise


    【解决方案1】:

    只需更改表名

    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: ""
    

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多