views:

31

answers:

2

I run script/generate model query

edit query.rb in models..

class Query < ActiveRecord::Base #I even tried Migrations instead of Base
  def sef.up
    create table :queries do|t|
      t.string :name
    end
  end

  def self.down
    drop_table :queries
  end
end

,run rake db:migrate.

and what I see in db is this:

mysql> desc queries;
+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| id         | int(11)  | NO   | PRI | NULL    | auto_increment | 
| created_at | datetime | YES  |     | NULL    |                | 
| updated_at | datetime | YES  |     | NULL    |                | 
+------------+----------+------+-----+---------+----------------+

Where is the "name" field?

HELP ! Cheers !

+1  A: 

Your data changes don't go in your model, they go in your migration. When you ran script/generate model, in addition to the query.rb file in models, you also would have gotten an XXX_add_queries.rb file in db/migrations. That's the file you need to edit to add another field.

However, if you've already run rake db:migrate, then you'll need to add a new migration to add your new field (script/generate migration add_name_to_queries), edit the new migration file, and run db:migrate again.

JacobM
Thank you so much!
Shreyas Satish
+2  A: 

What you want is script/generate migration your_migration_name.

Edit #1

I'll explain better:

Actually, when you run script/generate model query some files were created, including models/query.rb and db/migrate/XXX_create_query.rb. To create queries table you must edit the second file (db/migrate/XXX_create_query.rb) and insert the code you posted:

class CreateQueries < ActiveRecord::Migration
   def self.up
      create_table :queries do |t|
         t.string :name
      end
   end

   def self.down
     drop_table :queries
   end
end

and then run rake db:migrate.

Edit #2

As JacobM pointed, as you already run rake db:migrate, now you just need to create another migration

script/generate migration add_name_column_to_queries_table

edit the db/migrate/XXX_add_name_column_to_queries_table.rb file to insert your new name column and then run rake db:migrate again.

j.
Thank you so much!
Shreyas Satish