Forum: Ruby on Rails override RAILS_ENV from rake task

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7ab15538f9c9fbd4dbc19105fac4a864?d=identicon&s=25 Bryan Wilkerson (bryanw)
on 2007-07-20 20:23
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
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2007-07-20 21:53
(Received via mailing list)
You could do

rake RAILS_ENV="test" taskname

or are you looking for something more programmable?
7ab15538f9c9fbd4dbc19105fac4a864?d=identicon&s=25 Bryan Wilkerson (bryanw)
on 2007-07-20 21:56
Brian Hogan 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.
7ab15538f9c9fbd4dbc19105fac4a864?d=identicon&s=25 Bryan Wilkerson (bryanw)
on 2007-07-20 23:54
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'])
This topic is locked and can not be replied to.