Created
November 25, 2015 07:36
-
-
Save sampatbadhe/ca0ba00c12f00fac6ac8 to your computer and use it in GitHub Desktop.
Mapping example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# user = User.where(id: 1369).first | |
# contact_ids = Contact.where(creator_id: 1369).pluck(:id).sort | |
# kids = Kid.where("contact_id IN (:contact_ids)", contact_ids: contact_ids) | |
============================================================================ | |
old_db_kids = Kid.where("id IN (?)", kid_ids) | |
old_db_kid_ids = [1, 2, 3, 4, 5, 6, 7] | |
============================================================================ | |
new_db_kids = Kid.where("id IN (?)", kid_ids) | |
new_db_kid_ids = [4, 5, 6, 7] | |
============================================================================ | |
missing_kid_ids = old_db_kid_ids - new_db_kid_ids | |
missing_kid_ids = [1, 2, 3] | |
============================================================================ | |
contact_for_missing_contact_ids = Kid.where(id: missing_kid_ids).pluck(:contact_id) | |
contact_for_missing_contact_ids = [101, 102, 103] | |
contact_for_missing_contacts_old_db = Contact.where(id: contact_for_missing_contact_ids) | |
contact_for_missing_contacts_old_db = [101, 102, 103] | |
contact_for_missing_contacts_new_db = Contact.where(id: contact_for_missing_contact_ids) | |
contact_for_missing_contacts_new_db = [] | |
============================================================================ | |
suppose contact with id 103 is resotored with new id. | |
c_array = contact_for_missing_contacts_old_db.pluck_all(:id, :first_name, :last_name) | |
c_array = [[101, "Aaron", "Moy"], [102, "Abby", "Grenke"], [103, "Abhay", "Raj"]] | |
============================================================================ | |
map_hash = Hash.new | |
c_array.each{|c| map_hash[c[0]] = Contact.where(creator_id: 1369, first_name: c[1], last_name: c[2]).first.try(:id) } | |
map_hash = { 101=>nil, 102=>nil, 103=>113 } | |
============================================================================ | |
OLD_NEW_CONTACT_IDS = { 101=>101, 102=>101, 103=>113 } | |
============================================================================ | |
file = "#{Rails.root}/public/jeff_contacts_kids.csv" | |
CSV.open( file, 'w') do |writer| | |
kids.each do |kid| | |
next unless kid.present? | |
data_array = [ | |
kid.id, | |
kid.name, | |
OLD_NEW_CONTACT_IDS[kid.contact_id] | |
kid.created_at | |
kid.updted_at | |
] | |
writer << data_array | |
end | |
end | |
============================================================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment