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

  • Home
  • SEARCH
  • 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 9239571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:01:15+00:00 2026-06-18T08:01:15+00:00

I’m using rails v3.0.9 and Mysql database. I’m migrating the tables with existing data.

  • 0

I’m using rails v3.0.9 and Mysql database.

I’m migrating the tables with existing data.

I’ve users table and all other tables contains the user_id column and now i need seperate the users into two (users & customers) tables,

So i need to migrate the new customers table with existing users records where the user type with customer

I need to create a customers table and set the id of users records with user type as customer,
which will be easy instead of migrating many of other tables(which is used only by customers) by checking every record with user’s user type and assign the new id of customers table.

Tables looks like

users table:

    id  |       name    |   ...
   ------------------------------
    1   |       aaa     |   ...
    2   |       bbb     |   ...
    4   |       ddd     |   ...
    6   |       fff     |   ...

customers table

    id  |       name    |   ...
   -------------------------------
    3   |       ccc     |   ...
    5   |       eee     |   ...
    7   |       ggg     |   ...

When i’m migrating users existing data

In my migration file

def up
  create_table(:customers) do |t|
    t.string :name
  end

  User.joins(:user_type).where(:user_type => {:type_name => 'customer'}).find_in_batches(:batch_size => 100){ |users|
     users.each {|user|
          customer = Customer.new
          customer.id = user.id
          customer.name = user.name
          customer.save(:validate => false)
     }
   }
end

Also tried

Customer.create!(:id => user.id) instead of save method

Is this correct?

Is there any way to assign the primary id ?

If i’m going wrong give me some suggestion to do it in right way…

  • 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-18T08:01:16+00:00Added an answer on June 18, 2026 at 8:01 am

    Mysql allow to insert the record with value of primary id column.
    In Rails, you can execute any sql query by supplying to execute method

    For example:

    sql = "insert into users(id, username) values(10, 'iam')"
    ActiveRecord::Base.connection.execute(sql)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y’all
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am using jsonparser to parse data and images obtained from json response. When
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.