Topic: Add a column

Hello.

I am very new to rails. So my question would be surely be too basic.

I was asking myself, if the only thing I need to edit if I would add one or more columns to a table on my DB, are the attributes set in /models/ in the specific class and add those columns names in attr_accessible or should I do something more?

Well, I think it would be a good idea edit also the /db files shema.rb and the migration file.

Thank you!

Re: Add a column

create a new migration and to add the columns and check that the new columns are in the attr_accessible list (if that's what you want) then when you ruin the new migration the schema and the db will be updated for you

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Add a column

Thank you for your fast answer jamesw.

So, to be sure I have understood you (I'm not english so sometimes I need to be sure tongue), I just need to edit the migration file in /db/migrate adding the attributes I'd need and then run db:migrate, and everything else would be done by rails, right? Sometimes I also have doubts because it sounds too easy, but so it should be I think. big_smile

Re: Add a column

There is a saying in the world of Rails. "If you are finding it difficult then you are doing it wrong!"

you don't edit existing migration files, you create new ones and edit those. Rails takes care of the rest

From http://guides.rubyonrails.org/migrations.html

If the migration name is of the form “AddXXXToYYY” or “RemoveXXXFromYYY” and is followed by a list of column names and types then a migration containing the appropriate add_column and remove_column statements will be created.
$ rails generate migration AddPartNumberToProducts part_number:string

will generate
class AddPartNumberToProducts < ActiveRecord::Migration
  def change
    add_column :products, :part_number, :string
  end
end

Similarly,
$ rails generate migration RemovePartNumberFromProducts part_number:string

generates
class RemovePartNumberFromProducts < ActiveRecord::Migration
  def up
    remove_column :products, :part_number
  end

  def down
    add_column :products, :part_number, :string
  end
end

You are not limited to one magically generated column, for example
$ rails generate migration AddDetailsToProducts part_number:string price:decimal

generates
class AddDetailsToProducts < ActiveRecord::Migration
  def change
    add_column :products, :part_number, :string
    add_column :products, :price, :decimal
  end
end

As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit by editing the db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb file.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)