# Rake task error

**URL:** <https://www.ruby-forum.com/t/rake-task-error/205507>\
**Category:** Rails\
**Created:** [April 7, 2011, 7:58pm UTC](https://www.ruby-forum.com/t/rake-task-error/205507 "2011-04-07T19:58:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [April 7, 2011, 7:58pm UTC](https://www.ruby-forum.com/t/rake-task-error/205507/1 "2011-04-07T19:58:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 7, 2011, 10:57pm UTC](https://www.ruby-forum.com/t/rake-task-error/205507/2 "2011-04-07T22:57:35Z")

</div>

On Thu, Apr 7, 2011 at 12:58 PM, Ichiro S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ..  
> 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.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 7, 2011, 11:48pm UTC](https://www.ruby-forum.com/t/rake-task-error/205507/3 "2011-04-07T23: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:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 8, 2011, 12:07am UTC](https://www.ruby-forum.com/t/rake-task-error/205507/4 "2011-04-08T00:07:20Z")

</div>

On Thu, Apr 7, 2011 at 4:48 PM, Ichiro S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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. 🙂 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:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 8, 2011, 1:37am UTC](https://www.ruby-forum.com/t/rake-task-error/205507/5 "2011-04-08T01:37:26Z")

</div>

Bryan C. 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!
