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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:55:23+00:00 2026-06-02T09:55:23+00:00

Assuming I have 2 databases, main and temp , I have an ActiveRecord class

  • 0

Assuming I have 2 databases, main and temp, I have an ActiveRecord class Entry that corresponds to an entries table in main, how can I create a similar table in temp.

The first stab is use Entry.connection.execute('show create table #{Entry.table_name}') and invoke that against another table. (works if main and temp are same kind of database).

Is there a nicer way to do it? Preferably one that doesn’t involve reading & invoking “create table” expressions.

For example, something with ActiveRecord::Schema.define seems to be best. But I’m not sure exactly what to do.

Thanks.

  • 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-06-02T09:55:41+00:00Added an answer on June 2, 2026 at 9:55 am

    After quick googling I didn’t find ready-made method of achieving this without raw SQL. You can, however, make your own solution by mimicking what annotate_models does.

    A piece from https://github.com/ctran/annotate_models/blob/master/lib/annotate/annotate_models.rb

    # Use the column information in an ActiveRecord class
    # to create a comment block containing a line for
    # each column. The line contains the column name,
    # the type (and length), and any optional attributes
    def get_schema_info(klass, header, options = {})
      info = "# #{header}\n#\n"
      info << "# Table name: #{klass.table_name}\n#\n"
    
      max_size = klass.column_names.collect{|name| name.size}.max + 1
      klass.columns.each do |col|
        attrs = []
        attrs << "default(#{quote(col.default)})" unless col.default.nil?
        attrs << "not null" unless col.null
        attrs << "primary key" if col.name == klass.primary_key
    
        col_type = col.type.to_s
        if col_type == "decimal"
          col_type << "(#{col.precision}, #{col.scale})"
    

    Also, you might want to read what ActiveRecord Migrations Guide has to offer.

    ActiveRecord::Schema.define(:version => 20080906171750) do
      create_table "authors", :force => true do |t|
        t.string   "name"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
    end
    

    This file is created by inspecting the database and expressing its structure using create_table, add_index, and so on. Because this is database-independent, it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases.

    There is however a trade-off: db/schema.rb cannot express database specific items such as foreign key constraints, triggers, or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this, then you should set the schema format to :sql.

    Instead of using Active Record’s schema dumper, the database’s structure will be dumped using a tool specific to the database (via the db:structure:dump Rake task) into db/structure.sql. For example, for the PostgreSQL RDBMS, the pg_dump utility is used. For MySQL, this file will contain the output of SHOW CREATE TABLE for the various tables. Loading these schemas is simply a question of executing the SQL statements they contain. By definition, this will create a perfect copy of the database’s structure. Using the :sql schema format will, however, prevent loading the schema into a RDBMS other than the one used to create it.

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

Sidebar

Related Questions

Assuming we have a salt that's in the database and that has been generated
Assuming that best practices have been followed when designing a new database, how does
Assuming I have a comments model and a posts model, What code can I
Assuming I have an event subscription such as _vm.PropertyChanged += OnViewModelAmountChanged; How can I
Assuming I have a unix timestamp in PHP. How can I round my php
We have a custom connection string provider that connects to different databases based on
alt text http://img517.imageshack.us/img517/8124/56267347.jpg Assuming i have a winform that has a button called Create
I am trying to create a database that can be used to store lots
I have a question that relates to setting up the third table ( Repair_Parts
Assuming I have a column called A and I want to check if A

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.