How to use Rails framework in stand alone ruby file ??? (+)

0 views
Skip to first unread message

Falko

unread,
Jul 14, 2008, 4:29:18 AM7/14/08
to Ruby on Rails: Talk
Good day.

I want to run rb-script via Cron, for inseringt data in
database.
Is it possible using ActiveRecord, for accessing to DBMS. What
should I reguire in rb-file ?

Frederick Cheung

unread,
Jul 14, 2008, 5:58:40 AM7/14/08
to rubyonra...@googlegroups.com


There are slightly different answers depending on what you want to do.
I'm assuming that you do want to use your app's models (ie it's not
just activerecord you want, you need to setup the environment

The easiest way is to use script/runner, which will setup the
environment for you. Failing that, requiring config/boot and config/
environment will load up the rails environment for you. (those aren't
on the load path, so you'll need something like require
File.dirname(__FILE__) + '/../config/boot' depending on where your
script is in relation to the rest of the app)

Fred

Matthew Rudy Jacobs

unread,
Jul 14, 2008, 7:14:30 AM7/14/08
to rubyonra...@googlegroups.com

I'd suggest you may also want to make this script get run by a rake task

/lib/tasks/whatever.rake

desc "whatever you want"
task :something => :environment do
Call::The.code_you_want
end

then just get cron to call the rake command.
--
Posted via http://www.ruby-forum.com/.

Fernando Perez

unread,
Jul 14, 2008, 7:48:04 AM7/14/08
to rubyonra...@googlegroups.com
Hi,

require 'rubygems'
require 'active_record'


It's all in the wiki:
http://wiki.rubyonrails.org/rails/pages/HowToUseActiveRecordOutsideRails

Matthew Rudy Jacobs

unread,
Jul 14, 2008, 8:27:49 AM7/14/08
to rubyonra...@googlegroups.com
Fernando Perez wrote:
>
> require 'rubygems'
> require 'active_record'
>
>
> It's all in the wiki:
> http://wiki.rubyonrails.org/rails/pages/HowToUseActiveRecordOutsideRails
although,
that will only get you ActiveRecord...

it won't get you the connection setup in your database.yml,
or any of the subclasses of ActiveRecord::Base that you've set up in
your project.

Just depends what you're trying to do.

chalkers

unread,
Jul 14, 2008, 10:21:07 AM7/14/08
to Ruby on Rails: Talk
Creating a rake task is the far best solution. You can access all your
Models ideal for cron jobs. I use this on one of my projects which
updates/add many models every night.

On Jul 14, 12:14 pm, Matthew Rudy Jacobs <rails-mailing-l...@andreas-
Reply all
Reply to author
Forward
0 new messages