rake task error

Hi, guys. I wrote a rake task that called a method in model A.
in app.rake
  task :update_number => :environment do
    A.update_method
  end

in model A
  def self.update_method
    t1 = A.find.all
    ...
    ..
  end

When I ran the rake task, it returned an error "undefined local variable
or method t1". If I copied codes in the method to app.rake, it worked
perfectly. Did I miss something when I called the method in model from
rake?
Thanks in advance.

Have you put the following in your rake file?

require ‘active_record’
require ‘active_record/fixtures’

You need them in order for the rake file to have access to ActiveRecord.

B.

Thank you, Bryan. It works like a charm after adding the codes you
suggested! If I run the rake task in production mode, do I need to
change something in the rake file?

You’re welcome. :slight_smile: No need to change anything in file to run in production mode. A rake file will take the environment mode that the rails app is running in. If you need to run the rake file while rails is not running it’s going to look for the environment variable RAILS_ENV. Set that in your shell to the environment you want rake to run against and you’re good to go.

B.

Bryan Crossland wrote in post #991594:

You're welcome. :slight_smile: No need to change anything in file to run in
production
mode. A rake file will take the environment mode that the rails app is
running in. If you need to run the rake file while rails is not running
it's
going to look for the environment variable RAILS_ENV. Set that in your
shell
to the environment you want rake to run against and you're good to go.

B.

Got it. Thanks again and have a nice weekend!