Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple applications with Ruby on Rails

Is it possible to have a single 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!

like image 894
josef.van.niekerk Avatar asked Dec 06 '22 03:12

josef.van.niekerk


2 Answers

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!

like image 129
klew Avatar answered Dec 17 '22 09:12

klew


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!

like image 26
iOSDevil Avatar answered Dec 17 '22 08:12

iOSDevil