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
> 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 etcYou 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
On Jun 19, 7:55 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> 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... >Or require config/environment Fred
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 19, 7:55 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> 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... > > Or require config/environment > > Fred
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: http://daddy.platte.name/2007/07/hidden-in-plain-sight-scriptrunner.html I''ve used activeresource and various other things that I would have used script/runner for instead .. On Jun 19, 4:08 pm, Jeff Lewis <jeff.bu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Jun 19, 7:55 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> 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... > > > Or require config/environment > > > Fred