views:

137

answers:

1
class AddTitleToPosts < ActiveRecord::Migration
 def self.up
add_column :posts, :title, :string
    Post.find(:all).each do |post|
     post.update(:title => post.name.upcase)
    end
 end

 def self.down
 end

end

Like you can nothing particularly complicated, just trying to add new column title by changing case of name column already in db. But I get attempt to call private method error. I'm guessing it has something to do with 'self'?

Thanks for your help.

+1  A: 

Since you are adding a column you need to reset ActiveRecord's information about your model. The section Using a model after changing its table in ActiveRecord::Migration shows an example. This might work for you

class AddTitleToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :title, :string

    Post.reset_column_information

    Post.find(:all).each do |post|
      post.update(:title => post.name.upcase)
    end
  end

  def self.down
  end
end
Corey
That worked perfectly. Thanks
Senthil