Nate Bird
Nate Bird

Reputation: 5335

Loading Rails environment and classes from an independent gem

I have an app environment that we validate using an automated script. It populates some setup data in the Rails app and then sends a number of requests to validate some external workflows. Afterwards it removes the setup data from the database and cleans up other associated files.

The problem is I can’t have the scripts (currently rake tasks) in the application code. The script code needs to be independent from the application codebase for regulatory reasons.

Can anyone provide a way to load the Rails environment and classes in the rails application from a gem?

Right now I check the gem is run from the application root folder and then require the Rails environment.

require Dir.pwd + '/config/boot’

The issue is I can’t figure out how to reference the Rails app classes.

Upvotes: 2

Views: 1002

Answers (1)

Jesus Castello
Jesus Castello

Reputation: 1113

I think this is the proper way to load a Rails app (found in config.ru): require ::File.expand_path('../config/environment', __FILE__). In fact you can copy and paste that into a regular irb session to turn it into a 'rails console' session.

So the key seems to be to require your config/environment and that should allow you to use your app models normally.

Upvotes: 4

Related Questions