Passing schema name as a subdirectory not subdomain - ruby-on-rails

Hi I'am working on multi schema creation task, I have done with apartment gem and schema creation is working fine and now need to pass the schema name on the url as sub-directory not (subdomain) for eg: http://127.0.0.1:3000/schema_name.
If anyone has any suggestions or the way to do it please help me
I have tried with passing schema name as subdomain but can't able to pass it as sub-directory.

Related

Converting a single Tenant Application to a multitenant one using Apartment gem

I've built a single tenant rails application with all core features ready to ship. But I now want to make it multi-tenant using the apartment gem. Most tutorials I've found all show how to start from scratch. But I find no pointers on how to convert an existing project to have multi-tenancy built in. From my research I found that all models need a tenant ID added in the migration. How do I add this to all existing models easily? Does installing the gem and running the generator suffice? I'm running a rails 5 API application with close to 30 models and using graphql ruby in an Ubuntu 18.04 environment.
Any ideas on how to do this?
Thanks #lacostenycoder for pointing me in the right direction. Here's what I did.
Short answer: Yes, installing the gem, running the generator and creating your tenant model should suffice. Apartment reads your schema and creates schemas for the tenants.
Long answer, my experience:
I Backed up existing data from my database just in-case.
Installed apartment gem
If you are using any PostgreSQL database extensions like I was (pgcrypto, uuid- ssop)you need to understand that the extensions are not automatically loaded into the newly created schema.(look here for more details) do the following:
Follow instructions the github readme to create a shared_extensions schema
Create your first tenant. If it works, all is well. But...
If that doesn’t work, I mean when you create a tenant you get errors like ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist
or ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: function uuid_generate_v4() does not exist (more details here, here and here) like it was my case when creating my first tenant, I discovered that once the extensions are enabled on the public schema, they cannot be installed in the shared schema shared_extensions in this case. So you have to change them from the public schema to the shared_extensions schema. get more details here
If you are using uuid-ossp ALTER EXTENSION "uuid-ossp" SET SCHEMA shared_extensions in your rails dbconsole
If you are using pgcrypto ALTER EXTENSION "pgcrypto" SET SCHEMA shared_extensions in your rails dbconsole
The apartment gem will create all models for your tenant once you have it set up correctly and create your first tenant.
For more information on check out some of these github issues on apartment. Here and here.
Took me a day to figure it out and gather that info. I hope it saves you time and headache!
There doesn't appear to be any reason why you can't add this to an existing Rails app. I have not done this myself, nor does there appear to be any specific tutorials on how to migrate an existing app. Follow the gem setup instructions as shown on the README and also see the wiki for additional configurations and instructions. Use TDD wherever possible and expect to make changes to your application to get everything working correctly. Depending on how complex your existing app is will determine how much work this move will be.
You'll likely want to use lvh.me:3000 instead of localhost:3000 as root domain and port in your browser and perhaps use rails s -b lvh.me when starting your local server.
If you want to safely hack through this branch I recommend that you make a full backup of your local development database before you get started so that you can rollback to your stable current version in the event things go horribly wrong. If you're using Postgres for example there's pg_dump and pg_restore
This approach combined with good TDD should get you where you need to be.

How to initialize empty tables based on my rails schema without initializing in Rails?

I'm tasked to create a setup documentation from a project that will be passed on to new developers -- which needs them to setup the said project in their own systems.
Apparently, upon rake db:schema:load I found out that the project has model dependencies from files under /config/initializers which loads first before the models do.
I'm thinking that creating a script that would initialize empty tables first would be a good solution. Though I'm not sure how to do this without manually listing all the databases currently in use -- what if there is an additional table added in the future? This means I need to update the script everytime. Any way I can read the schema dynamically without actually running it (because, some models also need some things to be loaded from the config/initializers first.
Thanks!

Manage yml configs from active admin

I need to edit yml files from Active Admin (database.yml, memcached.yml and others).
Are there any gem to solve this problem? If no, how can it be solved? Or maybe there are any way to keep config data in database instead of yml (anything but database.yml)?
Modifying settings in yaml files that are checked into the repository doesn't sound like a good thing to do. I suggest to store settings in a datastore like a database using http://github.com/ledermann/rails-settings
See also Ruby on Rails - Storing application configuration

How to use restful_authentication for nested resources in Rails 2.3.8?

I have a nested resource hotel_user and i wish to have hotel_users_controller under hotel namespace.
I want to use restful authentication here and I want hotel_users_controller, sessions_controller in hotel directory like hotel/hotel_users_controller and hotel/sessions_controller. I tried with the following command in terminal and it failed.
script/generate authenticated hotel_user hotel/session
and it failed with the error
The name 'AuthenticatedSystem' is either already used in your application or reserved by Ruby on Rails.
Please choose an alternative and run this generator again.
The authenticated_system.rb file exists for some other resource under lib directory and not for hotel_user. the generator is seeing it and fails the generation of files. what can I do? please help
Change your file_name and module name.
Regenerate your restful_authentication and change it with other name. Or you previous file.

Editing a YAML file inside a Rails App

I'm trying to use a yaml config file as a very simple flat file db for a small rails app. The app has a single user, so I just need a way to store a username and password. The only thing is that I'd like to be able to edit the username and password while still inside the app and not require an app restart (so I can't load the YAML file inside an initializer...).
Any ideas on how I could accomplish this? I'm not married to the idea of using YAML, so if you have a better suggestion I'm all ears!
You really are better off using a database for this sort of thing, because it's how Rails is designed to work. The Rails default database is SQLite 3, which is a high-performance, reliable single file database.
Don't fight the defaults—use the right tool for the job.
You might wanna try iye for yaml editing on the fly. No DB needed, saves directly to file! Potentially you would just need something that tracks for file changes in your rails app and reloads your yaml file.
Here is the project page for iye: https://github.com/firmafon/iye
http://rubyforge.org/projects/rbyaml
http://yaml4r.sourceforge.net/doc/
(http://www.yaml.org/)

Resources