开发者

Plural to Singular conversion trouble in Rails Migrations?

I'm a beginner at Ruby On Rails and am trying 开发者_JAVA百科to get a migration to work with the name Priorities

So, here is the code I use in my migration:

class Priorities < ActiveRecord::Migration
  def self.up
    create_table :priorities do |t|
      t.column :name, :string, :null => false, :limit => 32
    end
    Priority.create :name => "Critical"
    Priority.create :name => "Major"
    Priority.create :name => "Minor"
  end

  def self.down
    drop_table :priorities
  end
end

This results in the following error though:

NOTICE:  CREATE TABLE will create implicit sequence "priorities_id_seq" for serial column "priorities.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "priorities_pkey" for table "priorities"
rake aborted!
An error has occurred, this and all later migrations canceled:

uninitialized constant Priorities::Priority

Is this some problem with turning ies to y for converting something plural to singular?

Also, the full --trace log is here: http://pastebin.com/w6usBSng


Using the following command, I was able to get the same error:

script/generate migration priorities

This is happening because you don't have a Priority class. You probably intended on running this command:

script/generate model Priority name:string

This fixes the problem


EDIT

Apparently you don't want a Priority model. In this situation, I have no idea why, but you can circumvent this by using execute in your migration methods.

Try something like this:

class CreatePriorities < ActiveRecord::Migration
  def self.up
    create_table :priorities do |t|
      t.column :name, :string, :null => false, :limit => 32
    end

    execute "insert into priorities (name) values ('Critical');"
    execute "insert into priorities (name) values ('Major');"
    execute "insert into priorities (name) values ('Minor');"

  end

  def self.down
    drop_table :priorities
  end
en

d


Yes. Your table name is Priorities and Model name also (i guess) Priorities. So it get crashed at "Priority.create :name => "Critical". This should be

class Priorities < ActiveRecord::Migration
  def self.up
    create_table :priorities do |t|
      t.column :name, :string, :null => false, :limit => 32
    end
    Priorities.create :name => "Critical"  #Where "Priorities" is your Model Name
    Priorities.create :name => "Major"
    Priorities.create :name => "Minor"
  end

  def self.down
    drop_table :priorities
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜