Share models between two apps?

Currently I am working on a REST API for a mobile app. I use rails-api and have some third party dependencies. Lately, one of my client asked to have admin panel, so that he could take a look at some data or edit them. I add activeadmin to the project, it works great, but I feel not right because originally I want my REST API project to be very slim and have less dependencies, but after I added activeadmin it has many dependencies. My app becomes big and it also takes more memory to run the app. We want to scale API part easily, and we don’t care much about admin panel because it’s only for one or two people who use it less often as well. That’s why I thought it would be nice to split into two projects and connect to the same DB, but I have no any experiences on sharing models that have some third parties dependency. For example: User model depends on devise, elasticsearch, and etc…

Have you had any experiences on splitting rails projects?

Yes. Very early into the lifetime of a public-facing Rails site, I realized I wanted the admin functionality to be different, so I made a new admin app.

I figured the models would be the same on both - so I symlinked the model directory of the admin app to the model directory of the public app.

This turned out to be a mistake, and one I’m still working on fixing. Eventually I realized that, by having the same file used in multiple apps, adding functionality in one would complicate things in the other.

(Another error in my thinking resulted from seeing all sorts of books and sites saying “thin controller, fat model”, which is terrible advice)

“User” was the perfect example. In my public-facing app I used the ‘authlogic’ gem for user authentication. Unfortunately this gem adds callbacks and validation rules to your user - things that make sense ONLY in the event that you’re creating / updating a user from a public page (password_confirmation, for example).

It meant that in the admin app I had to put workarounds in place to disable all the unwanted features that this gem added to my user, so that admins could edit and create users without any session-management silliness that simply wasn’t relevant.

So, to undo this mistake, I began by simply getting rid of the symlink and making two separate copies of the models directory, with the same files. When I have occasion to revisit one of those files I’ll read through at and figure out what’s irrelevant for the given app, and remove it.

It’s a slow process, and a year or so later there’s still plenty of unreachable code in both apps, that exist only because the other app wants it.

General recommendation:

  • Don’t share models.
  • Don’t share databases.
1 Like

It may be worth considering making use of the existing REST API in a separate, new app, that is purely for admin purposes.

The new admin app would have an entirely separate database. Most of the admin CRUD would happen through the REST API you’ve already built.

I will consider using the existing REST API for admin app. One interesting to think about is some action to be used only from admin, but not from mobile client app.