views:

35

answers:

2

For example I have the following migration

class AddStatusField < ActiveRecord::Migration
  def self.up
    add_column :tasks, :status, :string
    Task.update_all "status='complete'", "completed = 't'"
    remove_column :tasks, :completed
  end
end

When I run this (using rake db:migrate) I get the following message

==  AddStatusField: migrating =================================================
-- add_column(:tasks, :status, :string)
   -> 0.0010s
-- update_all("status='complete'", "completed = 't'")
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `update_all' for #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0xd3d34a8>

If I immediately run the migration it again the update_all calls appear to work.

I'm using Rails 2.3.5 and sqlite3.

What gives?

+1  A: 

Try adding

Task.reset_column_information

right before your update_all

Mr. Matt
That gives me: `undefined method 'reset_column_information' for #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0xd367598>`
Ben
Voldy, no, the second parameter `"completed = 't'"` is my condition (i.e. where-clause). Besides, the error message I'm getting is that `update_all` doesn't exist.
Ben
Ben, I see. My mistake. Thanks a lot! :)
Voldy
+3  A: 

It looks like it might be a naming conflict with Task. Does it fail with any other class in your application or just Task?

You might be able to call it using ::Task to indicate that you want the Task class in the root namespace. Rake, for example, defines its own Task class and it's possible that something in the migration process is doing the same.

Shadwell
Ah-ha, I think you might be right Shadwell. I tried prefixing with `::` but that made no difference. I just tested with a different model and that works fine. So... how can I avoid having to rename my `Task` model?
Ben
Sure enough, `Task` appears in http://wiki.rubyonrails.org/rails/pages/reservedwords. Dammnit! Thanks for the help.
Ben