4

I'm trying to run a migration on heroku and I can't seem to find the problem why my model class is not recognized.

This is my migration:

class AddTestToGoals < ActiveRecord::Migration
  def change
    add_column :goals, :test, :integer, default: 0, null: false
    Goal.reset_column_information
    Goal.all.each { |g| g.update_attribute :test, Goal::PASS }
  end
end

Running it using

heroku run rake db:migrate

and I get this error

uninitialized constant AddTestToGoals::Goal

Anyone knows what the problem is?

EDIT: miss typed before, it's the model which is not recognized, not the constant in it.

HALF WORKAROUND:

Using this (which I found here: http://visibletrap.blogspot.co.il/2011/10/heroku-access-railss-model-in-migration.html)

class AddTestToGoals < ActiveRecord::Migration
  class Goal < ActiveRecord::Base; end
  def change
     add_column :goals, :test, :integer, default: 0, null: false
     Goal.reset_column_information
     Goal.all.each { |g| g.update_attribute :test, Goal::PASS }
  end
end

heroku doesn't complain about not knowing what Goal is which solves half of the problem. but then, Goal::PASS is not recognized.

Oded
  • 233
  • 2
  • 9

3 Answers3

10

Old question, but I recently experienced something like it which was due to autoloading being disabled by setting

config.threadsafe!

in my environments/staging.rb file. It can be fixed by replacing it with the following

config.threadsafe! unless $rails_rake_task

This should be OK as there is no need for rake tasks to be threadsafe.

Simon Thordal
  • 893
  • 10
  • 28
0

EDIT:

Change all the references to Goal to be prefixed with ::.

::Goal.reset_column_information
::Goal.all.each { |g| g.update_attribute :test, ::Goal::PASS }
Finbarr
  • 31,350
  • 13
  • 63
  • 94
0

Unless I am missing something, all workarounds above have been blocked in later rails-versions. But one can still take control of their project/work from artificially-imposed constraints, by doing something like:

execute("INSERT INTO table_name (col_name) VALUES (your_values_here))")
JosephK
  • 668
  • 10
  • 18