在迁移中填充 Rails 类

发布于 2024-07-13 12:15:08 字数 888 浏览 4 评论 0原文

我正在尝试为一个仅用作枚举的简单表创建迁移。 所以我想立即用它的值填充表。 我尝试了以下操作:

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 技术交流群。

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

发布评论

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

评论(5

黎歌 2024-07-20 12:15:08

这是已经给出的两个答案的组合,但这应该对您有用:

class CreateUserRoles < ActiveRecord::Migration
  def self.up
    create_table :user_roles do |t|
      t.string :role
      t.timestamps
    end

    UserRole.create :role => "System administrator"
    UserRole.create :role => "Simulation controller"
  end

  def self.down
    drop_table :user_roles
  end
end

将您的类 UserType 重命名为 UserRole (以及关联的测试类,假设您使用生成器创建了所有这些)。 Rails 使用“type”列进行单表继承,并在您拥有从基类派生的模型时自动使用类名填充该字段。

This is a combination of two answers already given, but this should work for you:

class CreateUserRoles < ActiveRecord::Migration
  def self.up
    create_table :user_roles do |t|
      t.string :role
      t.timestamps
    end

    UserRole.create :role => "System administrator"
    UserRole.create :role => "Simulation controller"
  end

  def self.down
    drop_table :user_roles
  end
end

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.

妥活 2024-07-20 12:15:08

有点偏离主题,但值得一提:在迁移中实例化数据被认为是很糟糕的形式,因为在生产环境中创建数据库的“官方”方法

rake db:schema:load

当然不会从迁移文件加载数据。 您可能想看看我最喜欢的插件之一:Seed_Fu。

使用 Seed_fu,您可以在 YAML 中创建数据固定装置,然后发出“

rake db:seed

当您想要设置数据时”。

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

rake db:schema:load

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

rake db:seed

When you want to get the data set up.

爱的故事 2024-07-20 12:15:08

试试这个

class CreateUserTypes < ActiveRecord::Migration

  def self.up
    create_table :user_types do |t|
      t.string :role
      t.timestamps
    end

    UserType.create :role => "System administrator"
    UserType.create :role => "Simulation controller"
  end

  def self.down
    drop_table :user_types
  end

end

当您调用 rake db:migrate 时,默认运行的是 self.up 方法

编辑:将列名称更改为“角色”,因为“类型”是为单表继承保留的。 (看评论)。 向凯尔·布恩(Kyle Boon)道歉,因为他最终得到了非常相似的答案。

Try this

class CreateUserTypes < ActiveRecord::Migration

  def self.up
    create_table :user_types do |t|
      t.string :role
      t.timestamps
    end

    UserType.create :role => "System administrator"
    UserType.create :role => "Simulation controller"
  end

  def self.down
    drop_table :user_types
  end

end

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.

稀香 2024-07-20 12:15:08

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.

缪败 2024-07-20 12:15:08
  1. 将创建语句放在里面
    self.up
  2. 调用 UserType.reset_column_information
    创建表之后和之前
    填充它。 模型 UserType 是
    在创建表之前加载,所以
    Rails 已收集信息
    之前关于数据库结构
    表将存在。 你需要重置
    这样 Rails 就会检查表
    再次属性。
  3. 不要使用“type”作为属性名称。 它用于性传播感染。

    类CreateUserTypes < ActiveRecord::迁移

    def self.up 
        create_table :user_types 做 |t| 
          t.column:角色,:字符串 
          t.时间戳 
        结尾 
    
        UserType.reset_column_information 
    
        UserType.create :角色=>   “系统管理员” 
        UserType.create :角色 =>   《模拟控制器》 
    
      结尾 
    
      绝对自我贬低 
        drop_table:用户类型 
      结尾 
      

    end

  1. Place the create statements inside
    self.up
  2. Call 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.
  3. Don't use 'type' as an attribute name. Its used for STI.

    class CreateUserTypes < ActiveRecord::Migration

    def self.up
      create_table :user_types do |t|
        t.column :role, :string
        t.timestamps
      end
    
      UserType.reset_column_information
    
      UserType.create :role => "System administrator"
      UserType.create :role => "Simulation controller"
    
    end
    
    def self.down
      drop_table :user_types
    end
    

    end

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