Can't update records in my Rails database migrations
-
28-09-2019 - |
Question
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?
Solution
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.
OTHER TIPS
Try adding
Task.reset_column_information
right before your update_all
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow