Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails migration not working on heroku, Model class reported as uninitialized constant

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.

like image 997
Oded Avatar asked Dec 26 '22 22:12

Oded


1 Answers

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.

like image 188
Simon Thordal Avatar answered Jan 05 '23 18:01

Simon Thordal