# Override RAILS\_ENV from rake task

**URL:** <https://www.ruby-forum.com/t/override-rails-env-from-rake-task/109972>\
**Category:** Rails\
**Created:** [July 20, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/override-rails-env-from-rake-task/109972 "2007-07-20T20:23:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![railsroad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@railsroad](https://www.ruby-forum.com/u/railsroad)\
**Post date:** [July 20, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/override-rails-env-from-rake-task/109972/1 "2007-07-20T20:23:36Z")

</div>

I have a bunch of rake tasks that work against whichever database is  
associated with the current RAILS\_ENV. But there is another rake task  
that I want to invoke those db neutral tasks from that should only ever  
work against the test database.

How do I override RAILS\_ENV before invoking the database neutral tasks?

Thanks!

-b

---

<div class="post-metadata">

**Author:** ![railsroad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@railsroad](https://www.ruby-forum.com/u/railsroad)\
**Post date:** [July 20, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/override-rails-env-from-rake-task/109972/2 "2007-07-20T21:53:10Z")

</div>

You could do

rake RAILS\_ENV=“test” taskname

or are you looking for something more programmable?

---

<div class="post-metadata">

**Author:** ![railsroad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@railsroad](https://www.ruby-forum.com/u/railsroad)\
**Post date:** [July 20, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/override-rails-env-from-rake-task/109972/3 "2007-07-20T21:56:43Z")

</div>

Brian H. wrote:

> You could do
> 
> rake RAILS\_ENV=“test” taskname
> 
> or are you looking for something more programmable?

More programmable. I noticed in looking the standard Rails test tasks  
that they aren’t very DRY. There are a couple of places where code is  
repeated from the db namespace tasks apparently just to solve this  
problem.

---

<div class="post-metadata">

**Author:** ![railsroad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@railsroad](https://www.ruby-forum.com/u/railsroad)\
**Post date:** [July 20, 2007, 11:54pm UTC](https://www.ruby-forum.com/t/override-rails-env-from-rake-task/109972/4 "2007-07-20T23:54:14Z")

</div>

Bryan Wilkerson wrote:

> I have a bunch of rake tasks that work against whichever database is  
> associated with the current RAILS\_ENV. But there is another rake task  
> that I want to invoke those db neutral tasks from that should only ever  
> work against the test database.
> 
> How do I override RAILS\_ENV before invoking the database neutral tasks?
> 
> Thanks!
> 
> -b

I take back what I said about the test tasks. One of them provided me  
with the answer - you have to just set the connection.  
ActiveRecord::Base.establish\_connection(ActiveRecord::Base.configurations[‘test’])
