bbonamin
bbonamin

Reputation: 30793

Best practices when including Rails models in another application

I'm developing a ruby application that uses the models and data from another Ruby on Rails web application as its main data source.

The Rails models were included in this application by including the environment.rb file in the main file like this:

# Require Rails
 require_relative "../../RailsApp/config/environment.rb"

This works but there are uninitialized dependencies when loading models that use gems that are defined in the Rails Gemfile. (For example, acts_as_taggable_on, rack-pjax, devise, etc)

This ruby application dependencies are also managed through Bundler, so at the moment the only way to get the application working is to copy and paste the contents from the Rails' Gemfile into the ruby app's Gemfile.

Obviously this approach is not optimal as the gem requirements are duplicated.

Is there a better way to include Rails and the dependencies that its models require in another application? Is there a way to include a Gemfile into another?

Upvotes: 1

Views: 616

Answers (2)

Scott Schulthess
Scott Schulthess

Reputation: 2923

Here are some options, in order of simplicity

  1. Just keep everything in one app, a lot of stuff is easier this way
  2. Use plugins to share common code
  3. Use web services to share data

Upvotes: 6

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

You could extract the models and code out from RailsAppA into a Gem. RailsAppA then includes that Gem and uses it.

The gem can remain in a private repository and does not need published.

Both apps would then do something like:

gem "yourapp-modelage", git: "http://github.com/you/yourapp-modelage.git"

Then, App2 would also use that Gem... How much goes into the Gem will depends on how much you need to re-use.

Upvotes: 3

Related Questions