开发者

Multiple applications with Ruby on Rails

Is it possible to have a 开发者_如何学Csingle Ruby on Rails installations have multiple applications, that share a common model?

For example, I want to have a frontend application, as well as backend administration console, but both share the same model.

This is similar to the way Symfony works in PHP.

Thanks in advance!


The easiest way to have admin panel is to use namespaces. You just put all admin stuff to admin namespace. It is very common practice.

On the other hand, if you want to have two (or more) applications sharing the same database and models it is quite easy. I have one project that has two RoR applications sharing the same database. So here are my thoughts about it:

  1. I put all migrations in my first project. It can be messy if you put all of them (or few here few there) in both applications. Then, after migration, you can copy schema.db to second project, or just us a symbolic link (in Unix-like systems) and don't care about it anymore.
  2. If you want to share some model in both application, then you can copy model file or use symbolic link. I used first method, because I didn't want to copy all related models that I don't use in second application.
  3. It works great. But in this case you also have to setup server for two applications. I've used different subdomains for both applications.

Hope it helps!


klew's suggestion worked out for me. I have a fully featured admin backend and needed a lean and focus API app.

I ended up using the rails_api gem for the second app and created it under a separate user account.

I'm using postgres as my database.

Then I had to do the following: edit database.yml to use the same database, username, password as the other app rather than copy files I used ln -s I linked schema.rb and any model.rb files that I wanted to use in the API. At this point I was able to use rails console in the API app.

As I'm using the rails_api gem I was obviously missing routes. This is probably a good thing as I was able to manually create specific routes as required and not expose anything else to the web.

I do not do any migrations in the API app, everything except the odd linking of a model.rb file is done in my original app.

Seems to be working nicely, thanks Klew!


The most common way to run multiple application in RoR is using the engine architecture. It has been used in quite a few production applications.

Here is the details in RoR documentation http://guides.rubyonrails.org/engines.html

TaskRabbit's Example Code base which has an exceptionally detailed explanation about different approaches

Adding in this old question as i found this thread at first result as i searched google. Also the question is quite ambiguous as one might actually want a two totally decoupled application to run from the same RoR framework.(why? dont know...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜