Hello fellow programmers,
I have built a first application using devise and postgresql to sign up the admin for the exercise. The first application is for sign up purpose only.
I want to use the same admin model and the postgresql database from the first application and use it in the second application to login the admin into the portal, and not sign up.
Is there a way for me to achieve that?
When I try to create the same admin model and controllers using devise, it throws an error that the model already exists in the database when I try to create and migrate postgresql database.
You’re going to need to follow the pattern of choosing one app to do all the database migration changes, and let the other one be a “read-only” copy of it. You can copy the User/Admin/whatever model file out of the original app (where you used the CLI to generate your devise-enriched model) and paste it into the second app, as is.
You will need to copy the devise initializer file out of the first app’s config folder, and ensure that devise is listed in the gemfile of the second app, too. But you should not try to re-run the devise generator in the second app. As you noted, that will create a migration that you don’t want to run (and won’t run, as you saw). You should be able to just delete that migration file. Be sure to use the same secret key base (from the devise initializer) in both apps, otherwise the recover password stuff will fail in the second app.
Other than those details, this should work just fine for you. I’ve followed this pattern for a number of sites, where I have an “admin portal” to do content management and user admin, and a “public site” where users can sign in and do things with authentication/authorization control, using a single database for both applications.
Thanks for the advise. This is exactly what I was looking for. I will try to implement this. Just one thing do I run rails new . for the second app before copy pasting the files?
It depends on how different the two apps need to be. If you are just starting out, you could simply duplicate the project directory under a different name. That would ensure that you don’t have to do a lot of work around changing the database.yml and other configuration files and secrets to be identical in both projects.
If you expect the two sites to diverge sharply from one another, and have only the User model in common, you could do this duplication right now, while you only have that one model and Devise configured, and then do all your other development in the second (duplicate) project.
The things that need to be the same can stay untouched in both projects. If you do need to make updates to the User model through new migrations, you can decide then whether to make those changes in one project or the other.
There’s nothing inherently magical about rails new. It’s a macro for building out the skeleton of your project, and it sets a bunch of helpful defaults. It is very much designed around making a new, green-field project, separate and distinct from any other. secret_key_base and master.key and lots of other arcana will be globally unique, and while that’s excellent for two actually different projects, it is probably not what you want for this sort of “shared brain” setup with two applications and one database.
Give it a try, make sure you have a decent git history in both projects, and good luck! I know it will work, but I didn’t try doing this on my first app, so I may be forgetting some lessons learned along the way.
Thank you Walter that worked like a charm. I am able to login in the second app and it is pointing to the same database as the first one. I will now be able to add more models using devise and will not run in conflict with the admin model.