# rails environment in scripts dir

**URL:** <https://discuss.rubyonrails.org/t/rails-environment-in-scripts-dir/43617>\
**Category:** rubyonrails-talk\
**Created:** [June 19, 2009, 6:51pm UTC](https://discuss.rubyonrails.org/t/rails-environment-in-scripts-dir/43617 "2009-06-19T18:51:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jedrin](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jedrin](https://discuss.rubyonrails.org/u/Jedrin)\
**Post date:** [June 19, 2009, 6:51pm UTC](https://discuss.rubyonrails.org/t/rails-environment-in-scripts-dir/43617/1 "2009-06-19T18:51:06Z")

</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:** ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://discuss.rubyonrails.org/u/Philip_Hallstrom)\
**Post date:** [June 19, 2009, 6:55pm UTC](https://discuss.rubyonrails.org/t/rails-environment-in-scripts-dir/43617/2 "2009-06-19T18:55:12Z")

</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 &nbsp;&nbsp;&nbsp;def self.bar &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do whatever you want to do and have access to all your models, etc. &nbsp;&nbsp;&nbsp;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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [June 19, 2009, 7:23pm UTC](https://discuss.rubyonrails.org/t/rails-environment-in-scripts-dir/43617/3 "2009-06-19T19:23:53Z")

</div>

Or require config/environment

Fred

---

<div class="post-metadata">

**Author:** ![Jeff\_Burly](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jeff\_Burly](https://discuss.rubyonrails.org/u/Jeff_Burly)\
**Post date:** [June 19, 2009, 8:08pm UTC](https://discuss.rubyonrails.org/t/rails-environment-in-scripts-dir/43617/4 "2009-06-19T20:08:42Z")

</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| &nbsp;&nbsp;// do something with foo ... end ...

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

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

Jeff

---

<div class="post-metadata">

**Author:** ![Jedrin](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jedrin](https://discuss.rubyonrails.org/u/Jedrin)\
**Post date:** [June 22, 2009, 3:23pm UTC](https://discuss.rubyonrails.org/t/rails-environment-in-scripts-dir/43617/5 "2009-06-22T15:23:06Z")

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