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 61459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:19:21+00:00 2026-05-10T18:19:21+00:00

We are making a Ruby On Rails webapp where every customer gets their own

  • 0

We are making a Ruby On Rails webapp where every customer gets their own database.
The database needs to be created after they fill out a form on our website.

We have a template database that has all of the tables and columns that we need to copy. How can I do this in programatically from ruby on rails?

  • 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. 2026-05-10T18:19:21+00:00Added an answer on May 10, 2026 at 6:19 pm

    From any controller, you can define the following method.

     def copy_template_database         template_name = 'customerdb1' # Database to copy from         new_name = 'temp' #database to create & copy to          #connect to template database to copy.  Note that this will override any previous         #connections for all Models that inherit from ActiveRecord::Base         ActiveRecord::Base.establish_connection({:adapter => 'mysql', :database => template_name, :host => 'olddev',         :username => 'root', :password => 'password' })          sql_connection = ActiveRecord::Base.connection          sql_connection.execute('CREATE DATABASE #{new_name} CHARACTER SET latin1 COLLATE latin1_general_ci')         tables = sql_connection.select_all('Show Tables')         #the results are an array of hashes, ie:         # [{'table_from_customerdb1' => 'customers'},{'table_from_customerdb1' => 'employees},...]         table_names = Array.new         tables.each { |hash| hash.each_value { |name| table_names << name }}          table_names.each { |name|              sql_connection.execute('CREATE TABLE #{new_name}.#{name} LIKE #{template_name}.#{name}')             sql_connection.execute('INSERT INTO #{new_name}.#{name} SELECT * FROM #{template_name}.#{name}')         }         #This statement is optional.  It connects ActiveRecord to the new database         ActiveRecord::Base.establish_connection({:adapter => 'mysql', :database => new_name, :host => 'olddev',         :username => 'root', :password => 'password' })     end 

    Note that I do not know for sure if this will keep foriegn key integrity. I think it depends a lot on how the template Database is created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After making a few modifications to a rails app I am tinkering on, railroad
I'm making a simple to-do list application to teach myself Ruby on Rails, however
I am busy making a ruby on rails app. I want to make an
I am making Ruby On Rails 3.0.3 app with JQuery 1.4.4 and jQuery UJS
I am making a Ruby on Rails app and I need to be able
i using ruby on rails programing language for the making a very simple project
After making some changes in my models (eg. new field in a model and
This is not a ruby/rails project deploy. I have the following situation and I
I have recently begun the process of trying to learn Ruby/Rails and have been
With Ruby on Rails, REST is a great concept in terms of simplifying things

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.