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

前端 未结 5 791
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 05:24

I\'m currently building 2 API\'s using Ruby on Rails. One if for reading (finding objects, querying) and the other one is for actually writing to it, involving a kind of com

相关标签:
5条回答
  • 2020-12-29 05:40

    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

    0 讨论(0)
  • 2020-12-29 05:40

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

    rails new test1
    rails new test2
    cd test1
    rails 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/migrate
    cp ../test1/db/migrate/*_create_users.rb db/
    mv db/*_create_users.rb db/migrate/
    rake db:migrate
    rails 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

    0 讨论(0)
  • 2020-12-29 05:55

    You could try git submodule for that job.

    http://git-scm.com/book/en/Git-Tools-Submodules

    0 讨论(0)
  • 2020-12-29 05:57

    You could take a look at:

    • Best way to share ActiveRecord models and data between different Rails Apps?
    • Two rails apps sharing a model folder
    • Shared models between two Rails apps - what is the ideal solution for Workflow?
    0 讨论(0)
  • 2020-12-29 06:01

    My trick for doing this is to not actually use Rails tricks. I use "git" tricks, and pull in code from a 3rd, shared-code repo. I put this into both apps as an engine, and as an external git reference.

    It's a little more work, but once you have done it once in one app, it's easy to use that as a template for the next one, too.

    0 讨论(0)
提交回复
热议问题