# rake task error

**URL:** <https://discuss.rubyonrails.org/t/rake-task-error/58318>\
**Category:** rubyonrails-talk\
**Created:** [April 7, 2011, 5:58pm UTC](https://discuss.rubyonrails.org/t/rake-task-error/58318 "2011-04-07T17:58:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 7, 2011, 5:58pm UTC](https://discuss.rubyonrails.org/t/rake-task-error/58318/1 "2011-04-07T17:58:16Z")

</div>

Hi, guys. I wrote a rake task that called a method in model A. in app.rake &nbsp;&nbsp;task :update\_number =\> :environment do &nbsp;&nbsp;&nbsp;&nbsp;A.update\_method &nbsp;&nbsp;end

in model A &nbsp;&nbsp;def self.update\_method &nbsp;&nbsp;&nbsp;&nbsp;t1 = A.find.all &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;.. &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Bryan\_Crossland](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@Bryan\_Crossland](https://discuss.rubyonrails.org/u/Bryan_Crossland)\
**Post date:** [April 7, 2011, 8:55pm UTC](https://discuss.rubyonrails.org/t/rake-task-error/58318/2 "2011-04-07T20:55:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 7, 2011, 9:48pm UTC](https://discuss.rubyonrails.org/t/rake-task-error/58318/3 "2011-04-07T21:48:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Bryan\_Crossland](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@Bryan\_Crossland](https://discuss.rubyonrails.org/u/Bryan_Crossland)\
**Post date:** [April 7, 2011, 10:05pm UTC](https://discuss.rubyonrails.org/t/rake-task-error/58318/4 "2011-04-07T22:05:11Z")

</div>

You’re welcome. 🙂 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.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 7, 2011, 11:37pm UTC](https://discuss.rubyonrails.org/t/rake-task-error/58318/5 "2011-04-07T23:37:26Z")

</div>

Bryan Crossland wrote in post #991594:

> You're welcome. 🙂 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!
