在迁移中填充 Rails 类
我正在尝试为一个仅用作枚举的简单表创建迁移。 所以我想立即用它的值填充表。 我尝试了以下操作:
class CreateUserTypes < ActiveRecord::Migration
def self.up
create_table :user_types do |t|
t.column :type, :string
t.timestamps
end
end
def self.down
drop_table :user_types
end
UserType.create :type => "System administrator"
UserType.create :type => "Simulation controller"
end
但出现此错误:
rake aborted!
An error has occurred, all later migrations canceled:
Could not find table 'user_types'
我正在关注 Rails wiki 和期望它能起作用。
谢谢。 但你的建议似乎不起作用。 嗯,我看不到弦。
sqlite> select * from user_types;
1||2009-02-08 12:00:56|2009-02-08 12:00:56
2||2009-02-08 12:00:57|2009-02-08 12:00:57
I'm trying to create a migration for a simple table that is just used as an enum. So I want to populate the table immediately with its values. I tried the following:
class CreateUserTypes < ActiveRecord::Migration
def self.up
create_table :user_types do |t|
t.column :type, :string
t.timestamps
end
end
def self.down
drop_table :user_types
end
UserType.create :type => "System administrator"
UserType.create :type => "Simulation controller"
end
but I get this error:
rake aborted!
An error has occurred, all later migrations canceled:
Could not find table 'user_types'
I was following the Rails wiki and expected it to work.
Thanks. But what you suggested doesn't seem to be working. Well, I can't see the strings.
sqlite> select * from user_types;
1||2009-02-08 12:00:56|2009-02-08 12:00:56
2||2009-02-08 12:00:57|2009-02-08 12:00:57
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是已经给出的两个答案的组合,但这应该对您有用:
将您的类 UserType 重命名为 UserRole (以及关联的测试类,假设您使用生成器创建了所有这些)。 Rails 使用“type”列进行单表继承,并在您拥有从基类派生的模型时自动使用类名填充该字段。
This is a combination of two answers already given, but this should work for you:
Rename your class UserType to UserRole (along with the associated test classes, assuming you created all of these using the generators). Rails uses the 'type' column for single table inheritance, and automatically populates the field with a class name when you have models that derive from a base class.
有点偏离主题,但值得一提:在迁移中实例化数据被认为是很糟糕的形式,因为在生产环境中创建数据库的“官方”方法
当然不会从迁移文件加载数据。 您可能想看看我最喜欢的插件之一:Seed_Fu。
使用 Seed_fu,您可以在 YAML 中创建数据固定装置,然后发出“
当您想要设置数据时”。
A bit off topic but worth mentioning: It is considered poor form to instantiate data in migrations because the "official" way to create databases in the production environment is
which of course will not load your data from a migration file. You may want to have a look at one of my favorite plugins for this: Seed_Fu.
With Seed_fu, you create your data fixtures in YAML and then issue a
When you want to get the data set up.
试试这个
当您调用 rake db:migrate 时,默认运行的是 self.up 方法
编辑:将列名称更改为“角色”,因为“类型”是为单表继承保留的。 (看评论)。 向凯尔·布恩(Kyle Boon)道歉,因为他最终得到了非常相似的答案。
Try this
It is the self.up method that runs by default when you call rake db:migrate
Edit: changed column name to 'role' as 'type' is reserved for single table inheritance. (See comments). Apologies to Kyle Boon for ending up with a very similar answer.
HermanD 的答案应该有效。 但我想指出的是,您应该小心使用名为 'type' 的列 AFAIK ,Rails 使用该列名称来表示 STI。
HermanD's answer should work. I want to point out though, that you should be careful about using a column named 'type' AFAIK , Rails uses that column name for STI.
self.up
UserType.reset_column_information
创建表之后和之前
填充它。 模型 UserType 是
在创建表之前加载,所以
Rails 已收集信息
之前关于数据库结构
表将存在。 你需要重置
这样 Rails 就会检查表
再次属性。
不要使用“type”作为属性名称。 它用于性传播感染。
类CreateUserTypes < ActiveRecord::迁移
end
self.up
UserType.reset_column_information
after creating the table and before
populating it. The model UserType is
loaded before creating the table, so
rails has collected information
about database structure before the
table will exists. You need to reset
it so Rails will inspect table
attributes again.
Don't use 'type' as an attribute name. Its used for STI.
class CreateUserTypes < ActiveRecord::Migration
end