3

I'm running a custom rake task...

namespace :import do

  desc "Import terms of service as HTML from stdin"
  task :terms => :environment do
    html = STDIN.read
    settings = ApplicationWideSetting.first
    settings.terms_and_conditions = html
    if settings.save
      puts "Updated terms of service"
    else
      puts "There was an error updating terms of service"
    end
  end

end

The model ApplicationWideSetting is reported as undefined when running the task in the production environment. However, when running the task on other environments (ie. development, staging, test.) the task runs fine.

Running the process in rails console, in all environments, completes ok.

Does anyone know what's going on, things I could check?

note: I ran the task with

puts Rails.env 

To check the shell environment var RAILS_ENV was getting set/read correctly. I've also tried both with and without the square brackets around the :environment dependency declaration.

additional info: Rails v3.2.14

further info: I've setup a completely fresh rails app, and the script works fine in any environment. Since the install in question is a real production environment, I'll have to setup another deploy and check it thoroughly. More info as I find it.

5
  • rake import:terms RAILS_ENV=production??
    – beck03076
    Aug 29, 2013 at 9:28
  • I've exported RAILS_ENV= production and puts it to stdout to ensure its set. I'll give that a try anyway, but it'd be very odd if it works...
    – ocodo
    Aug 29, 2013 at 10:39
  • @beck03076 predictably that had no effect.
    – ocodo
    Aug 29, 2013 at 10:40
  • What happens if you remove the brackets from environment? I typically only see it as task :terms => :environment.
    – d_ethier
    Aug 29, 2013 at 12:24
  • @d_ethier - I added the bracketing in a desperate attempt to try and fix it! this was happening originally with the standard generated :environment dependency. As this is only failing under the production environment, and the shell environment var is successfully set, I guess there's something in the config of production that's causing the fault... Problem is, I don't know what the culprit might be since it runs fine as server and console.
    – ocodo
    Aug 29, 2013 at 20:37

1 Answer 1

7

In a nutshell, Rails doesn't eager load models (or anything else) when running rake tasks on Production.

The simplest way to work with a model is to require it when you begin the rake task, and it should work as expected, in this case:

# explicitly require model
require 'application_wide_setting'

It's possible to eager load the entire rails app with:

Rails.application.eager_load!

However, you may have issues with some initializers (ie. devise)

1
  • 1
    Thank you so much. This also helps e.g. when you want to access different STI models using the base class.
    – Rip-Off
    Dec 2, 2014 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.