From: John Butler Date: 2008-05-29T21:49:20+09:00 Subject: Re: Merge collections of objects Ron Fox wrote: > Unless this is a toy project, it will be important to think about what > is it that makes two employees, one in companyA and one in companyB > 'duplicates'...and what you really want to do in that case as the > record information related to the emplyees may be different between > the companies...all this is interesting business logic.. not a ruby > question however. > > RF Yes ive figured that out. Its proving a bit of pain especially with has many through relationships. For the above i have a company that has many employees through company_employees so what i basically want to do is. companyA has 2 employees "a", "b" companyB has 3 employees "a", "b", "c" Copy any employees from companyB that are not in companyA Delete all reference to employees from companyB So companyA will have 3 employees "a", "b", "c" companyB will have no employees Ive tried various tings as there are a lot of associations i need to copy over so im trying to create one method that will deal with this passing through the 2 objects and the relationship in dyn_method: dyn_method = 'employees' myarray = Array.new myarray = companyA.send(dyn_method) | company_b.send(dyn_method) companyA.send(dyn_method + '=',myarray) The below works as in it updates all the company_id links in the company_employees table with companyA's id so the employees are only linked to companyA and not referenced by companyB anymore. The duplicates causes an issue here though. dyn_method = 'company_employees' companyA.send(dyn_method + '=',companyB.send(dyn_method)) Im still looking for the best solution for adding to a company which has no employees and then to a company that already has employees but ignoring the duplicates. JB -- Posted via http://www.ruby-forum.com/.