0

I am having a strange problem where my rake task is running on my local machine but not in production. Getting the following error when try the same in PRODUCTION

rake aborted!uninitialized constant User::UserGroup

when I ran the trace on it got following output

$ RAILS_ENV=production bundle exec rake billing:invoices --trace

** Invoke billing:invoices (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute billing:invoices
Building invoices
rake aborted!
uninitialized constant User::UserGroup
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/base.rb:1205:in `compute_type'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/reflection.rb:162:in `klass'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:2107:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:1984:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:1984:in `build_join_association'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:1948:in `build'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:1956:in `block in build'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:1955:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:1955:in `build'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/associations.rb:1852:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/relation/query_methods.rb:246:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/relation/query_methods.rb:246:in `build_joins'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/relation/query_methods.rb:176:in `build_arel'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/relation/query_methods.rb:149:in `arel'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/relation/batches.rb:51:in `find_in_batches'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.11/lib/active_record/relation/batches.rb:20:in `find_each'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p327/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p327/bin/rake:23:in `<main>'
Tasks: TOP => billing:invoices

The strange part is It is working all OK on my Local machine. Any suggestions ?

Arihant Godha
  • 2,339
  • 2
  • 26
  • 50

2 Answers2

1

My guess is that you have just added a migration called something like 20120221123456_users.rb, and within it, you have:

class SomeNameThatsNotUsers < ActiveRecord::Migration
  ...

When Rails executes a migration, it expects to execute the file, which defines the class, and then instantiate the class, and call #up or #down on that class instance. How does Rails know what class to instantiate? It's supposed to correspond with the part of the file name following the numeric prefix and underscore, so for a file name like 20120221123456_users.rb, the class name must be Users.

Sumit Munot
  • 3,748
  • 1
  • 32
  • 51
0

Can you put the rake task source code that is failing?

Usually you need to write something like this (to avoid having issues with AR):

desc "My Task description" task :my_task_name => :environment do # Task code here end

rorra
  • 9,593
  • 3
  • 39
  • 61
  • Thanks @rorra fixed the issue by changing this in my config/production.rb file from this config.threadsafe! unless ENV['THREADSAFE'] == 'off', to this --config.threadsafe!, config.dependency_loading = true if rails_rake_task. But still unclear with this what this will do ? – Arihant Godha Mar 04 '13 at 10:24