开发者

How to share tables between two separate ruby on rails applications?

开发者 https://www.devze.com 2023-04-05 03:54 出处:网络
I am working on a web app with a public site and an admin site. They both share a table : Users, the model of which is in the public site. How can I share this table 开发者_开发知识库with the admin ap

I am working on a web app with a public site and an admin site. They both share a table : Users, the model of which is in the public site. How can I share this table 开发者_开发知识库with the admin app?


If I were you, I wouldn't separate the public and the admin functionality. Instead build both of them as part of the same site, just namespace the admin routes and control who gets access to admin functionality via something like CanCan (although you can hand-roll a solution quite easily as well).

If you can't do that then, as one of the other answers pointed out, you can create a duplicate User model in your admin site and override the connection for that model to point to your public database. So in your config/database.yml, you need to add a database configuration for you public database:

public_production:
  adapter: postgresql
  database: public_production
  username: blah
  password:
  host: localhost

#this is your existing production db
production:
  adapter: postgresql
  database: admin_production
  username: blah
  password:
  host: localhost

Then in your duplicate User model in your admin site:

class User < ActiveRecord::Base
  establish_connection "public_#{RAILS_ENV}"
end

Your duplicate model will now be pulling data from your public database. You will also likely need to define a development and test version of your public database in your database.yml e.g.:

public_development:
  adapter: postgresql
  database: public_development
  username: blah
  password:
  host: localhost

If you don't want to duplicate your User model, you can pull it out into a separate gem and make that gem a Rails engine. You will then be able to include that gem into both your public and admin site and have the model be available in both. However if you do things this way you will need to have the extra database definitions in both your public and your admin project (since the User model will still need the establish_connection bit).


You could create a RESTful webservice on top of the model, and call this from the other app. Or you could build a separate User app with such a service that you call from both the other apps.

For a deeper look into this kind of design I recommend the book Service-Oriented Design with Ruby and Rails by Paul Dix.


If you go with setting up two AR connections, it involves:

  1. Create another UserOther model
  2. Override establish connection

Examples: Multiple database connection in Rails

Docs: http://apidock.com/rails/ActiveRecord/Base/establish_connection/class

Best regards


Creating two different databases may be a hassle. (ex. When your hosting your apps on heroku). You can do the same thing within a unique database.

Lets imagine I have two apps A & B.

For each you define the database.yml to point to the same database.

Then on each project in application.rb, add

config.active_record.table_name_prefix = "a_"

and respectively for app B

config.active_record.table_name_prefix = "b_"

All tables will have a prefix. ex:

a_posts
b_transactions

If you want app B to access a table created for a example the user table,

class User < ActiveRecord::Base
     self.table_name = "a_users"
end

It works like a charm.

0

精彩评论

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