Share Models between 2 Rails API's (Separate Applications) Share Models between 2 Rails API's (Separate Applications) ruby ruby

Share Models between 2 Rails API's (Separate Applications)


The way I would do it is a 'Mountable Engine'. Check out the excellent Railscast by Ryan Bates for starters and the engine-section at api.rubyonrails.org for further details.

With best regards, Mandi


If you just want to share models, you can add the other project models folder into your autoload paths:

rails new test1rails new test2cd test1rails g model User cd ../test2/# ACTION REQUIRED: edit config/application.rb adding this line# inside the class Application < Rails::Application block:## config.autoload_paths += %W(#{config.root}/../test1/app/models)#mkdir db/migratecp ../test1/db/migrate/*_create_users.rb db/mv db/*_create_users.rb db/migrate/rake db:migraterails r 'puts User.inspect' #=> User(id: integer, created_at: datetime, updated_at: datetime)

You can also set the whole thing in order to have the two app/models folders as private, using a third shared folder, adding this to the projects:

# config.autoload_paths += %W(/path/to/a/shared/folder)

This folder can even be not the same folder for each project, so it could be a path to a git submodule , for example (if you use GIT, I reccomend this solution).

Another option could be pointing app/models to a shared folder with a soft link