Forum: Ruby on Rails Accessing Rails Models/app from command line script

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.
John T. (Guest)
on 2006-04-28 04:20
(Received via mailing list)
Hi,

Can someone point me to some info on how to access Rails models /
ActiveRecord classes from a Ruby command line script?

Say I have a plain text file that is put into some directory by an
outside process, and I want to have a Ruby script run, via a cron job,
that can open and parse that data file. It would then delete all
records in some lookup table in my database and load it with the data
from the text file.

Hope that made sense!  Thanks!

jt
Benjamin C. (Guest)
on 2006-04-28 18:23
(Received via mailing list)
script/runner can do this for you.

--
Benjamin C.
http://www.tesly.com/ -- Collaborative test case management
http://www.agilewebdevelopment.com/ -- Resources for the Rails community
John T. (Guest)
on 2006-04-28 18:51
(Received via mailing list)
On 4/28/06, Benjamin C. <removed_email_address@domain.invalid> wrote:
> script/runner can do this for you.

Hi,

Thanks. I did find reference to that. It looks like it can run a
method of a model(? .. Book.dosomething). Is it able to run methods of
controllers? I guess that could work with what I have in mind. If it
can only run model-based methods, I suppose I can access other models
from the one I'm running the script for.

(anyone got any examples out there?)

Thanks,

jt
Brian H. (Guest)
on 2006-04-28 19:53
(Received via mailing list)
On Apr 28, 2006, at 10:41 AM, John T. wrote:

> On 4/28/06, Benjamin C. <removed_email_address@domain.invalid> wrote:
>> script/runner can do this for you.
>
> Thanks. I did find reference to that. It looks like it can run a
> method of a model(? .. Book.dosomething).

You could also run a method available in any classes you have in your
lib directory, since those are automatically loaded when your Rails
app loads. I use this all the time and I keep meaning to move my
custom "model" classes to /lib. :)

> Is it able to run methods of controllers?

No. Your controller methods (aka. actions) only run in the context of
a web request. If you really need to do that, you could use a command-
line tool like curl, or wget, to make an http request into your Rails
app. Personally, I'm not really a fan of this.

> I guess that could work with what I have in mind. If it can only
> run model-based methods, I suppose I can access other models from
> the one I'm running the script for.

Again, don't think about it as just "model-based" methods. The
methods that you call don't actually have to do anything with your
model classes, if you don't want them to. Although, that kind of
thing would be a little strange. :)

> (anyone got any examples out there?)

Sure. Here's a class I use for performing a data export from the
command line. I use this, largely as a crutch, when I don't feel like
building an admin interface into one of my survey-esque Rails apps.

require 'csv'
class DataExport
   def self.export_ratings
     path = "#{RAILS_ROOT}/tmp/my_export.csv"
     count = 0
     File.open( path, "w+" ) do |p|
       CSV::Writer.generate(p, ',', "\r\n") do |csv|
         csv << Student.cols_to_csv
         count += 1
         Student.get_completed.each do |student|
           courses = student.to_csv_list
           courses.each do |course|
             count += 1
             csv << course
           end
         end
       end
     end
     count
   end
end

In case anyone is wondering about the count variable, I actually use
that for my unit tests on this method. It's a quick way to make sure
the export file has the correct number of lines, which tells me that
the entire export process has worked. There are also tests on the
Student class method "cols_to_csv" and instance method "to_csv_list".

-Brian
John T. (Guest)
on 2006-04-28 20:18
(Received via mailing list)
On 4/28/06, Brian H. <removed_email_address@domain.invalid> wrote:

> You could also run a method available in any classes you have in your
> lib directory, since those are automatically loaded when your Rails
> app loads. I use this all the time and I keep meaning to move my
> custom "model" classes to /lib. :)

Ahh.. Cool. Didn't think of that. That would work quite well.

> Sure. Here's a class I use for performing a data export from the
> command line. I use this, largely as a crutch, when I don't feel like
> building an admin interface into one of my survey-esque Rails apps.

...

Thanks for the example too!

jt
This topic is locked and can not be replied to.