导轨迁移

发布于 2024-07-15 09:24:17 字数 371 浏览 2 评论 0原文

我的迁移如下:

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

当我进入脚本/控制台并输入“User”时,Rails 无法识别该类。

My migration is as follows:

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

When I go to script/console and type "User", Rails does not recognize the class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-07-22 09:24:17

您是否运行了 script/generate model User ...script/generate migration CreateUser...

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

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

我希望这有帮助。

Did you run script/generate model User ... or script/generate migration CreateUser...?

If you do not generate the model, it won't be available in the console, as Rails doesn't know it exists.

Rails also does not create a modelname_id field, it simply creates an id field which autoincrements.

I hope this helps.

梦萦几度 2024-07-22 09:24:17

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

2)您需要在 app/model/user.rb 文件中声明此类

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

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

1) The migration will create an auto-incrementing "id" column. (I've never seen a migration create a class_id column unless it was specified).

2) You will need to declare this class in a app/model/user.rb file

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

More importantly I want to recommend the restful_authentication plugin. It's the community standard for user authentication (meaning it's battle tested, regularly updated, and conforms to most use cases).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文