small medium large xlarge

Generic-user-small
26 Jul 2008, 18:04
Aaron Miller (1 post)

When I generated my scaffold, I created ratings as strings. I would like to change that, so that ratings are now stored as decimals. I tried changing the schema and migration files (as shown below), but this does not seem to work. Suggestions?

FROM:

create_table “posts”, :force => true do |t| t.string “rating” t.datetime “created_at” t.datetime “updated_at” end

class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.string :rating t.timestamps end

TO:

create_table “posts”, :force => true do |t| t.decimal “rating” t.datetime “created_at” t.datetime “updated_at” end

class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.decimal :rating t.timestamps end

Thanks,

Aaron

Generic-user-small
26 Jul 2008, 21:26
James West (104 posts)

Personally I would generate a new migration to change the column type. This is covered in the book somewhere! then run the migration to phyiscally change the column.

M_v_shokhirev_pragsmall
29 Sep 2008, 16:42
Mikhail V. Shokhirev (a.k.a. Mike Shock) (19 posts)

To change ‘rating’ from string to decimal you should (1) script/generate migration ChangePosts

(2) describe the changing of the column in the generated migration file: class ChangePosts < ActiveRecord::Migration; def self.up; change_column :posts, :rating, :decimal; end; def self.down; change_column :posts, :rating, :string; end; end;

(3) run the migration by ‘ rake db:migrate ‘

You must be logged in to comment