Here is the rake task:
task :map_comment_to_user => :environment do
Comment.all.each do |c|
a = Admin.where(mongo_id: c.mongo_user_id).first
u = User.where(email: a.email).first
c.user_id = u.id
c.save
end
end
I screwed up a migration from Admin => User tables, and the email being unique allows me to stitch them back together. But it's so slow.
How do I write the above in straight PSQL?
UPDATE comments c
SET c.user_id = u.id
FROM users u admins a
WHERE c.mongo_user_id == a.mongo_id
AND u.email = a.email; ???
Your SQL is very close. It just require a few minor syntax changes.
UPDATE comments c
SET user_id = u.id
FROM users u, admins a
WHERE c.mongo_user_id = a.mongo_id
AND u.email = a.email;