Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3311768
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:54:56+00:00 2026-05-17T21:54:56+00:00

Stuff I’ve already figured out I’m learning how to create a multi-tenant application in

  • 0

Stuff I’ve already figured out

I’m learning how to create a multi-tenant application in Rails that serves data from different schemas based on what domain or subdomain is used to view the application.

I already have a few concerns answered:

  1. How can you get subdomain-fu to work with domains as well? Here’s someone that asked the same question which leads you to this blog.
  2. What database, and how will it be structured? Here’s an excellent talk by Guy Naor, and good question about PostgreSQL and schemas.
  3. I already know my schemas will all have the same structure. They will differ in the data they hold. So, how can you run migrations for all schemas? Here’s an answer.

Those three points cover a lot of the general stuff I need to know. However, in the next steps I seem to have many ways of implementing things. I’m hoping that there’s a better, easier way.

Finally, to my question

When a new user signs up, I can easily create the schema. However, what would be the best and easiest way to load the structure that the rest of the schemas already have? Here are some questions/scenarios that might give you a better idea.

  1. Should I pass it on to a shell script that dumps the public schema into a temporary one, and imports it back to my main database (pretty much like what Guy Naor says in his video)? Here’s a quick summary/script I got from the helpful #postgres on freenode. While this will probably work, I’m gonna have to do a lot of stuff outside of Rails, which makes me a bit uncomfortable.. which also brings me to the next question.
  2. Is there a way to do this straight from Ruby on Rails? Like create a PostgreSQL schema, then just load the Rails database schema (schema.rb – I know, it’s confusing) into that PostgreSQL schema.
  3. Is there a gem/plugin that has these things already? Methods like "create_pg_schema_and_load_rails_schema(the_new_schema_name)". If there’s none, I’ll probably work at making one, but I’m doubtful about how well tested it’ll be with all the moving parts (especially if I end up using a shell script to create and manage new PostgreSQL schemas).

Thanks, and I hope that wasn’t too long!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:54:56+00:00Added an answer on May 17, 2026 at 9:54 pm

    Update Dec 5, 2011

    Thanks to Brad Robertson and his team, there’s the Apartment gem. It’s very useful and does a lot of the heavy lifting.

    However, if you’ll be tinkering with schemas, I strongly suggest knowing how it actually works. Familiarize yourself with Jerod Santo’s walkthrough , so you’ll know what the Apartment gem is more or less doing.

    Update Aug 20, 2011 11:23 GMT+8

    Someone created a blog post and walks though this whole process pretty well.

    Update May 11, 2010 11:26 GMT+8

    Since last night I’ve been able to get a method to work that creates a new schema and loads schema.rb into it. Not sure if what I’m doing is correct (seems to work fine, so far) but it’s a step closer at least. If there’s a better way please let me know.

    module SchemaUtils
      def self.add_schema_to_path(schema)
        conn = ActiveRecord::Base.connection
        conn.execute "SET search_path TO #{schema}, #{conn.schema_search_path}"
      end
    
      def self.reset_search_path
        conn = ActiveRecord::Base.connection
        conn.execute "SET search_path TO #{conn.schema_search_path}"
      end
    
      def self.create_and_migrate_schema(schema_name)
        conn = ActiveRecord::Base.connection
    
        schemas = conn.select_values("select * from pg_namespace where nspname != 'information_schema' AND nspname NOT LIKE 'pg%'")
    
        if schemas.include?(schema_name)
          tables = conn.tables
          Rails.logger.info "#{schema_name} exists already with these tables #{tables.inspect}"
        else
          Rails.logger.info "About to create #{schema_name}"
          conn.execute "create schema #{schema_name}"
        end
    
        # Save the old search path so we can set it back at the end of this method
        old_search_path = conn.schema_search_path
    
        # Tried to set the search path like in the methods above (from Guy Naor)
        # [METHOD 1]: conn.execute "SET search_path TO #{schema_name}"
        # But the connection itself seems to remember the old search path.
        # When Rails executes a schema it first asks if the table it will load in already exists and if :force => true. 
        # If both true, it will drop the table and then load it. 
        # The problem is that in the METHOD 1 way of setting things, ActiveRecord::Base.connection.schema_search_path still returns $user,public.
        # That means that when Rails tries to load the schema, and asks if the tables exist, it searches for these tables in the public schema.
        # See line 655 in Rails 2.3.5 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
        # That's why I kept running into this error of the table existing when it didn't (in the newly created schema).
        # If used this way [METHOD 2], it works. ActiveRecord::Base.connection.schema_search_path returns the string we pass it.
        conn.schema_search_path = schema_name
    
        # Directly from databases.rake. 
        # In Rails 2.3.5 databases.rake can be found in railties/lib/tasks/databases.rake
        file = "#{Rails.root}/db/schema.rb"
        if File.exists?(file)
          Rails.logger.info "About to load the schema #{file}"
          load(file)
        else
          abort %{#{file} doesn't exist yet. It's possible that you just ran a migration!}
        end
    
        Rails.logger.info "About to set search path back to #{old_search_path}."
        conn.schema_search_path = old_search_path
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some stuff in settings.py that I'd like to be able to access
Why would a coder stuff things into __dict__ that can't be used for attribute
My stuff is made with several components among which some are written in C.
I save stuff in an Isolated Storage file (using class IsolatedStorageFile). It works well,
Various programs can do stuff only when you haven't used the computer for a
I see stuff like this a lot: interface A { ... } interface B
private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return
For my personal stuff I just use the svnadmin hotcopy command once a week
Pretty straightforward stuff, here -- I'm just not good enough with mysql to understand
In reading Haskell-related stuff I sometimes come across the expression tying the knot, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.