views:

322

answers:

2

Hi, I'm a beginner at Ruby On Rails and am trying 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

+3  A: 

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

macek
Can you give a reference to this convention? I've never seen such a thing
Earlz
The convention is made apparent by using `script/generate model foo`. The migration that is automatically created is called `class CreateFoos < ActiveRecord::Migration`.
macek
@macek It isn't that I don't want a model, it's that I just didn't create one yet. I thought Migrations and Models were completely different.. Yea, from now on I'm not trusting Komodo Edit to enter in commands for me with it's macro magic..
Earlz
@Earlz, I wouldn't abandon all resolve just yet; you just meant `model` instead of `migraiton` in this specific case.
macek
Yea true. I'm new to rails and new to the editor so it's a recipe for disaster :P
Earlz
you can do it using "execute" but it's not a More Rails way Use ModelName instead.
Salil
+2  A: 

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
Salil
I didn't think a Model was required for a migration? I didn't have a Model created before for it, but now I do and it still doesn't work. And I tried changing to `priorities` and I get a `undefined method 'create'` error
Earlz
The model name should be singular `Priority`. As evidence by his class name `Priorities` (instead of the conventional `CreatePriorities`, he generated the migration manually using `script/generate migration priorities`. This did not create a `prirority.rb` model for him. Using `script/generate model priority ...` fixes the issue.
macek
This is the correct answer?
macek
@macek what the. I could've swore I chose your's changed it now though
Earlz