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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:Then in your duplicate User model in your admin site:
Your duplicate model will now be pulling data from your public database. You will also likely need to define a
developmentandtestversion of your public database in yourdatabase.ymle.g.: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_connectionbit).