我的迁移如下:
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无法识别该类。
答案 0 :(得分:4)
您是否运行了script/generate model User ...
或script/generate migration CreateUser...
?
如果你没有生成模型,它将无法在控制台中使用,因为Rails不知道它存在。
Rails也不会创建modelname_id
字段,它只会创建一个自动增量的id
字段。
我希望这会有所帮助。
答案 1 :(得分:3)
1)迁移将创建一个自动递增的“id”列。 (除非已指定,否则我从未见过迁移创建class_id列。)
2)您需要在app / model / user.rb文件中声明此类
class User < ActiveRecord::Base
#class methods go here
end
更重要的是,我想推荐restful_authentication插件。它是用户身份验证的社区标准(意味着它经过了战斗测试,定期更新,并且符合大多数用例)。