Resue main application models and database with other rails application

Viewed 125

I have a rails(3) app(Master), which has the schema, models and database. Also I have one other rails(5) app(service) which doesn't have any models but I am using (master) app database.

service app configuration

common_db:
  adapter: postgresql
  database: 'blog'
  username: 'username'
  password: 'password'
  host: 'localhost'
  pool: '10'

application_record.rb

 class ApplicationRecord < ActiveRecord::Base
   self.abstract_class = true
   establish_connection     
   Rails.configuration.database_configuration["common_db"]
 end

then I added one same model in service app then trying to get some values from master application

class post < ApplicationRecord
    has_many :comments
    scope :article_comments, lambda { |a| select("created_at").joins('INNER JOIN comments on post.id = comments.post_id').where('article_id = ?', 
    a) }
end

and if I call Post.article_comments here this joins and all is not working, absolutely this will not work as we don't have any association or any models in service app.

Basically my question is

1) can I reuse this master database model with service app?

2) If I can use this then can I use it with all associations Example: Blog.comments in service app .....

3) Also is there any other good approach to reuse this, also I saw some articles https://hiltmon.com/blog/2013/10/14/rails-tricks-sharing-the-model/, this will be a good thing to do all?

4 Answers

Remember that the relations are set in the models, you can have the same database, but need to have the models with the relations there to make this work.

the link that you put on point 3, they just created a rake task to copy some files from one app to another, this copy models for the purpose of having relations there and make it work. it's the same concept as above, but with a rake task to make the copy of files.

1) Yes, you can reuse master database model with service app (really easy to do on Heroku).

2) Yes, you need to add the models so the relations exist, you don't need all your models but that may cause some troubles (like running the scope you declare in post's model where you need comment's model).

3) The link you provide is a good way to make sure you have your models up to date. You could also write a rake task in you master app to update the models and trigger it every time you make a commit.

To answer your questions, yes, all of what you are asking is possible, as Remigio Arenas Muñoz de Cote answered.

However, another (and arguably more semantic) approach would be to make your shared models into a gem that you can share between as many projects as you want, keeping a single codebase.

This article might explain in more depth than I could do here: http://www.akitaonrails.com/2016/10/03/sharing-models-between-rails-apps-part-1

Hope it helps.

Literally follow the steps in the article you linked. Your master models need to be mirrored into your service object. You are going to run into some hiccups since you're dealing with Rails 5 vs 3 (newest 5.2, for example, assumes that belongs_to must be present [build in validation]).

So if you can, get both apps onto the same version that will be easier to sync them. Otherwise you're going to need to maintain the models and tests between them more or less by hand.

You can also look at having the old app setup an API and talk to that from the new one. That way you won't need all the models, just the relevant items that the service is concerned with.

Related