Ximik
Ximik

Reputation: 2495

Rails + foreign app

I have rails website that shows information from database. And I have ruby script that should add data in this database. Can I somehow connect this script to my rails app to use its models?

Upvotes: 0

Views: 49

Answers (3)

Dogbert
Dogbert

Reputation: 222168

You can either load the Rails environment by doing a require

require File.dirname(__FILE__) + '/config/environment'

or use a Rake task (http://railscasts.com/episodes/66-custom-rake-tasks).

Upvotes: 1

rtdp
rtdp

Reputation: 2107

You can add this scripts under lib tasks, tell your rails application to include lib files. Inside those files, you can access rails models to insert data in database.

After that you may run those scripts by calling from rake tasks. Create some rakes under lib/tasks.

Those rake tasks can again be called via cron jobs to work repeatedly for you.

Upvotes: 1

Michael De Silva
Michael De Silva

Reputation: 3818

You could write a rake task and load in your data to the DB. Yes, your rake task can reference models and you have access to all the associated AR methods etc. It goes without saying that you can also run the rake task on development, production etc.

Upvotes: 1

Related Questions