【问题标题】:Rails MigrationRails 迁移
【发布时间】:2010-10-16 04:00:51
【问题描述】:

我的迁移如下:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :email
      t.string :password
      t.string :name
      t.boolean :male
      t.boolean :admin

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

当我转到脚本/控制台并键入“用户”时,Rails 无法识别该类。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord migration


    【解决方案1】:

    1) 迁移将创建一个自动递增的“id”列。 (除非指定,否则我从未见过迁移创建 class_id 列)。

    2) 你需要在 app/model/user.rb 文件中声明这个类

    class User < ActiveRecord::Base
      #class methods go here
    end
    

    更重要的是我想推荐restful_authentication 插件。它是用户身份验证的社区标准(意味着它经过实战测试、定期更新并符合大多数用例)。

    【讨论】:

      【解决方案2】:

      您运行的是script/generate model User ... 还是script/generate migration CreateUser...

      如果您不生成模型,它在控制台中将不可用,因为 Rails 不知道它存在。

      Rails 也不会创建 modelname_id 字段,它只是创建一个自动递增的 id 字段。

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-18
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 2014-03-24
        • 2020-10-28
        • 2015-03-01
        • 2011-10-02
        相关资源
        最近更新 更多