jack
jack

Reputation: 454

Rails execute script

I am building a script in on of my controllers to fill a database with an excel files data. I would build the function, then access it through a route. (That i guess i can protect with cancan) But i thought about it, and it doesn't seem very ... 'Railsy'.

I know the scripts folder exists, and it is probably for these kinds of tasks. I've tried googling stuff like 'rails execute script' and other stuff, but i can't find any good advice for what to do next.

I'm sorry if this seems kind of stupid, but in my apps i've been kind of hacking around stuff to make it work, so any advice on this task would be appreciated.

Upvotes: 1

Views: 612

Answers (2)

Dave Newton
Dave Newton

Reputation: 160170

If you need to upload the file in the app and process it, it should probably go in the "lib"directory and be accessed like any other Ruby library/module/etc.

If it's something you need to run locally, "on demand", "scripts" is fine. If you need access to your rails environment when running it like any Rails models, you can run it from "rails console" or "rails runner".

As Aln said, there are a variety of ways it could be scheduled as well.

Upvotes: 1

Ain Tohvri
Ain Tohvri

Reputation: 3035

You could simply do

#!/usr/bin/env ruby
require 'rubygems'

# regular ruby code here

and have it running just like any other util. Of course you can always call any *.rb with simply

ruby somescript.rb

If you need some scheduled script, check into rufus-scheduler gem.

Upvotes: 0

Related Questions