Ruby on Rails Thursday, January 31, 2013

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.

My tables should 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, I can't assign the id of user as
customer's primary id

In my migration file

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

User.joins(:user_type).where(:user_type => {:type_name =>
'customer'}).find_in_batches(:batch_size => 100){ |users|
users.each {|user|
customer = = = => false)

Also tried

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


INSERT INTO customers(id, name) VALUES("#{}", "#{}")

Finally all ended with same error

Error: Called id for nil, which would mistakenly be 4 -- if you really
the id of nil, use object_id

Is this correct?

Is there any way to assign the primary id ?

Please anyone explain how to do this?

Posted via

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
For more options, visit

No comments:

Post a Comment