andkjaer
andkjaer

Reputation: 4065

Move data from one table to another

I need to move some user info from one db table to another.
From User to brands_users.

I want to move user_id and brand_id to brands_users - how do I do that?

How do i save data to a HABTM DB table?

Thanks

Upvotes: 0

Views: 3449

Answers (2)

Syed Aslam
Syed Aslam

Reputation: 8807

Assumptions:

  1. You have models User, Brand and the relationship model BrandsUser.
  2. You have brand_id in the users table.
  3. Rails 3.

Write a script which will read users table and create records in the brands_users table

class BrandsUser << AR::Base
  belongs_to :user
  belongs_to :brand
end

require 'rubygems'

User.each do |u|
  bs = BrandsUser.new(
    :user_id => u.id,
    :brand_id => u.brand_id
  )
  bs.save
end

Run the script using rails runner

rails runner db/scripts/data_mover.rb

Upvotes: 4

naresh
naresh

Reputation: 2113

insert into brands_users select user_id, brand_id from ......

Upvotes: 0

Related Questions