views:

783

answers:

3

I have created a new table including a column "note". The default is varchar(255) I believe but I wish to have this column be a text area vs. a field and to allow more data. I imagine that I would make this change in ActiveRecord::Migration file but I am curious as to the format. Do I simply change the varchar(255) to varchar(1000) for example? (if so what is the format?

def self.up
    create_table :notes do |t|
      t.string :note :varchar(1000)
    end

Is that the right format? Furthermore, how do I get the entry field to be multiple rows. Sorry if this is easy stuff but I am new to programming and RoR. Thanks.

+2  A: 

You can simply use the 'text' type instead of 'string'.

def self.up
  create_table :notes do |t|
    t.text :note
  end
end

Using the 'text' type will result in database column of type TEXT. Varchar is usually limited to a maximum length of 255 (in MySQL, other RDBMSs have similar limits).

If you use Rails' form helpers, a textarea will be output for this field (because it is of type 'text'). textarea is the form element that accepts multi-line input.

Edit: If you've already migrated the create_table, you can create a new migration to change the column type:

def self.up
  change_column :notes, :note, :text
end
Siddhartha Reddy
Can I just changed the ..._create_notes.rb file that was originally generated? Then do rake db:migrate to implement this change? Or will that cause problems and I should do a removal of the old column or a change?
bgadoci
If you've already migrated, you either need to drop the database, recreate it and migrate (which destroys all data in the database) or you need change columns like Ben suggest. Otherwise, if you haven't migrated yet, just change the line in your migration and run the migration.
EmFi
@bgadoci: If you've never called db:migrate after this migration was generated, you can simply edit the file before calling db:migrate. Otherwise, you can create a new migration to make the change: def self.up change_column :notes, :note, :text end
Siddhartha Reddy
Is this where I put it?class CreateNotes < ActiveRecord::Migration def self.up create_table :notes do |t| t.string :note t.string :prospect change_column :notes, :note, :textarea t.timestamps end end
bgadoci
Actually that didn't work. Where do I put it?
bgadoci
Damn, comments don't have any code formatting! I've edited the answer to include the code from the above comment.
Siddhartha Reddy
Thanks man. I really appreciate it. I know I am a pain but could I ask you if the terminal command is script/generate migration change_column , etc. Not familiar with this yet.
bgadoci
Generate a new migration using the command 'script/generate migration ChangeNoteTypeInNotes' and then put in the above change_column snippet of code code in the file it generates (file will be located in db/migrate/). And then call 'rake db:migrate'.
Siddhartha Reddy
That is exactly what I needed. Thanks for breaking it down. Sorry to be so needy! I am three days into this.
bgadoci
Rails provides decent inline help for its scripts that you might find useful. Just invoke the commands without any arguments to access this help. For example, 'scrip/generate migration' shows help on how to create migrations.
Siddhartha Reddy
Thanks again. I really appreciate it.
bgadoci
+1  A: 

You can change the length with the limit option as so...

def self.up
  change_column :notes, :note, :string, :limit => 1000
end
Ben
Do I have to make this change through the terminal first by executing a script/generate migration ... command and then edit the new file? Or can I simply open the original migration file and change, save, and rake db:migrate?
bgadoci
If you this is one of the last tables created, you are in development mode, etc, then rollback. Check schema.rb to see the colums are no longer listed, then edit the migration, and run db migrate again. This won't work if you have data in the field, etc.
Tom Andersen
+1  A: 

The correct format would be


t.string :note, :limit => 1000

make sure you are using a version of MySQL(or whichever database) which supports varchars longer than 256 characters.

if you want to use a large text block it would be


t.text :note

See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html for more information

The Who