# Rails environment in scripts dir

**URL:** <https://www.ruby-forum.com/t/rails-environment-in-scripts-dir/170629>\
**Category:** Rails\
**Created:** [June 19, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/rails-environment-in-scripts-dir/170629 "2009-06-19T20:52:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/rails-environment-in-scripts-dir/170629/1 "2009-06-19T20:52:11Z")

</div>

Is there an easy way I can write an independent script that gets the  
rails environment the way console does etc ? I’ve never seen this  
except through like backgroundrb etc

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/rails-environment-in-scripts-dir/170629/2 "2009-06-19T20:56:21Z")

</div>

> Is there an easy way I can write an independent script that gets the  
> rails environment the way console does etc ? I’ve never seen this  
> except through like backgroundrb etc

You could create say lib/foo.rb and within that have say:

class Foo  
def self.bar  
# do whatever you want to do and have access to all your models,  
etc.  
end  
end

And run it with “./script/runner Foo.bar”

You could also do it as a rake task and have rake load up the  
environment…

-philip

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/rails-environment-in-scripts-dir/170629/3 "2009-06-19T21:24:47Z")

</div>

On Jun 19, 7:55 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> environment…

Or require config/environment

Fred

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 22, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/rails-environment-in-scripts-dir/170629/4 "2009-06-22T17:24:44Z")

</div>

How long has script/runner been part of rails ?  
I guess either it was not well advertised or my learning of ruby/rails  
somehow overlooked that.

This guy posted something to that effect:

> **[Hidden in plain sight: script/runner](http://daddy.platte.name/2007/07/hidden-in-plain-sight-scriptrunner.html)**
>
> I am loving the opportunity to work with my co-workers in the home office. We have "geek-out" lunches in the office once a week or so, and i...

I’ve used activeresource and various other things that I would have  
used script/runner for instead …

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/rails-environment-in-scripts-dir/170629/5 "2009-06-19T22:09:45Z")

</div>

Or another alternative is to create some runnable script and run it  
against the environment you want:

$ cat ./script/do\_some\_foo.runnable

puts " fetching all foo in #{RAILS\_ENV} env:"  
Foo.find(:all).each do |foo|  
// do something with foo …  
end  
…

$ ./script/runner ./script/do\_some\_foo.runnable  
fetching all foo in development env:  
…

$ ./script/runner -e production ./script/do\_some\_foo.runnable  
fetching all foo in production env:  
…

Jeff

On Jun 19, 12:23 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
