Alex Rivera | Logout

Two rails apps sharing a model folder

Asked 2009-03-18T21:47:59.670
22

I have two rails apps running off the same database, one which runs the client and one which provides an admin interface.

Both the apps have the exact same models defined save for small number of differences. Its tiresome for me to duplicate the vast majority of changes in the models on to both apps.

One way for two apps to use the same model info is to symlink the model folder from one app to the other, but I cant do that due to the few differences in code (e.g an extra validation on the client).

Is there a simple way I can move out the differences so I can keep the common code in one place?

Edit
Report

2 Answers

2

Yes, symlink the models directory, but then add an environment variable to your environment.rb for each instance. Then your models can know which instance is using it and include additional validations or what not.

in environment.rb:

APP_INSTANCE = "app1"

in model.rb

validates_length_of :name, :within => 3..100, :if => :is_app_one?

def is_app_one?
    APP_INSTANCE == "app1"
end
answered 2009-06-01T18:27:13.887
1

Why not using composition instead of inheritance?

I have a similar issue : an admin app and a public app, with the same database, and only some specific methods on some models for one app or the other.

I'm currently thinking I could create a common place with many modules where I would put my methods. I would then include the modules I need in each app (in models, controllers, helpers, …) when I need them. This place could be in the lib directory (updated with a git submodule or svm external) or in a gem (updated with Bundler or a similar tool).

What do you think about this ?

answered 2011-02-02T14:29:05.883

Your Answer