Hi, I wonder if there is a quick way to do this. companyA has 2 employees "a", "b" companyB has 3 employees "a", "b", "c" I basically want to merge companyA employess with companyB employees so when i look at companyA the employees will equal "a", "b", "c" i know i could do something like companyA.employees = companyB.employees but this would just overwrite companyA employees so if companyA had more employees than companyB i would lose the employees companyA had that companyB didnt. I think companyA.employees << companyB.employees would just mash them together and give me duplicates. The only other way i can think off is a nasty double loop that compares each object 1 by 1 and then starts again going through the process. JB -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
what about using the "+" and "uniq!" methods (associations are just arrays after all)? companyA = (companyA.employees + companyB.employees).uniq! This may be brittle because I''m counting on the fact that the association is an array. On May 28, 7:00 am, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I wonder if there is a quick way to do this. > > companyA has 2 employees "a", "b" > > companyB has 3 employees "a", "b", "c" > > I basically want to merge companyA employess with companyB employees so > when i look at companyA the employees will equal "a", "b", "c" > > i know i could do something like companyA.employees = companyB.employees > but this would just overwrite companyA employees so if companyA had more > employees than companyB i would lose the employees companyA had that > companyB didnt. > > I think companyA.employees << companyB.employees would just mash them > together and give me duplicates. > > The only other way i can think off is a nasty double loop that compares > each object 1 by 1 and then starts again going through the process. > > JB > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
should be: companyA.employees = (companyA.employees + companyB.employees).uniq! On May 28, 2008, at 10:05 AM, Hardbap wrote:> > what about using the "+" and "uniq!" methods (associations are just > arrays after all)? > > companyA = (companyA.employees + companyB.employees).uniq! > > This may be brittle because I''m counting on the fact that the > association is an array. > > > On May 28, 7:00 am, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Hi, >> >> I wonder if there is a quick way to do this. >> >> companyA has 2 employees "a", "b" >> >> companyB has 3 employees "a", "b", "c" >> >> I basically want to merge companyA employess with companyB >> employees so >> when i look at companyA the employees will equal "a", "b", "c" >> >> i know i could do something like companyA.employees = >> companyB.employees >> but this would just overwrite companyA employees so if companyA had >> more >> employees than companyB i would lose the employees companyA had that >> companyB didnt. >> >> I think companyA.employees << companyB.employees would just mash them >> together and give me duplicates. >> >> The only other way i can think off is a nasty double loop that >> compares >> each object 1 by 1 and then starts again going through the process. >> >> JB >> -- >> Posted viahttp://www.ruby-forum.com/. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 28 May 2008, at 15:05, Hardbap wrote:> > what about using the "+" and "uniq!" methods (associations are just > arrays after all)? > > companyA = (companyA.employees + companyB.employees).uniq! > > This may be brittle because I''m counting on the fact that the > association is an array. >It also won''t work as uniq! returns nil if there are no duplicates. Use uniq instead. There''s also the | operator irb(main):005:0> [1,2,3]|[1,2,3,4,5] => [1, 2, 3, 4, 5] Fred> > On May 28, 7:00 am, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Hi, >> >> I wonder if there is a quick way to do this. >> >> companyA has 2 employees "a", "b" >> >> companyB has 3 employees "a", "b", "c" >> >> I basically want to merge companyA employess with companyB >> employees so >> when i look at companyA the employees will equal "a", "b", "c" >> >> i know i could do something like companyA.employees = >> companyB.employees >> but this would just overwrite companyA employees so if companyA had >> more >> employees than companyB i would lose the employees companyA had that >> companyB didnt. >> >> I think companyA.employees << companyB.employees would just mash them >> together and give me duplicates. >> >> The only other way i can think off is a nasty double loop that >> compares >> each object 1 by 1 and then starts again going through the process. >> >> JB >> -- >> Posted viahttp://www.ruby-forum.com/. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for correcting the "un. The | operator is more succinct. Thanks Fred. On May 28, 2008, at 10:22 AM, Frederick Cheung wrote:> > > On 28 May 2008, at 15:05, Hardbap wrote: > >> >> what about using the "+" and "uniq!" methods (associations are just >> arrays after all)? >> >> companyA = (companyA.employees + companyB.employees).uniq! >> >> This may be brittle because I''m counting on the fact that the >> association is an array. >> > It also won''t work as uniq! returns nil if there are no duplicates. > Use uniq instead. > There''s also the | operator > irb(main):005:0> [1,2,3]|[1,2,3,4,5] > => [1, 2, 3, 4, 5] > > Fred > >> >> On May 28, 7:00 am, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >>> Hi, >>> >>> I wonder if there is a quick way to do this. >>> >>> companyA has 2 employees "a", "b" >>> >>> companyB has 3 employees "a", "b", "c" >>> >>> I basically want to merge companyA employess with companyB >>> employees so >>> when i look at companyA the employees will equal "a", "b", "c" >>> >>> i know i could do something like companyA.employees >>> companyB.employees >>> but this would just overwrite companyA employees so if companyA had >>> more >>> employees than companyB i would lose the employees companyA had that >>> companyB didnt. >>> >>> I think companyA.employees << companyB.employees would just mash >>> them >>> together and give me duplicates. >>> >>> The only other way i can think off is a nasty double loop that >>> compares >>> each object 1 by 1 and then starts again going through the process. >>> >>> JB >>> -- >>> Posted viahttp://www.ruby-forum.com/. >>> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---