Multiple Singular Conversion Error in Rails Migrations?
I am new to Ruby On Rails and am trying to get to working with a name Priorities
So, here's the code I'm using 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:
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
This is a problem with the translation ies
in y
to convert something into a single plural?
Also, the full log --trace
is here: http://pastebin.com/w6usBSng
a source to share
Using the following command, I was able to get the same error:
script/generate migration priorities
This is because you don't have a class Priority
. You probably intended to run this command:
script/generate model Priority name:string
This fixes the problem
EDIT
Apparently you don't need a model Priority
. In this situation, I have no idea why, but you can get around this by using execute
migrations in your 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
a source to share
Yes. The name of your table is the priorities and the name of the model as well (I think). Priorities. So it breaks down to "Priority.create: name =>" Critical ". It 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
a source to share