Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find matches in two arrays in IRB?

For some reason I can't figure this out. But basically I want to compare to models and see if they have any matching emails. Here's my attempt, but this doesn't for some reason work at all.

>> CardReferral.all.select{|cf|cf.email == CardSignup.all.collect{|cs|cs.email}}

Where I can somehow return the object..

like image 431
Trip Avatar asked Dec 06 '22 00:12

Trip


1 Answers

CardReferral.all.map(&:email) & CardSignup.all.map(&:email)

from the rdoc

array & other_array Set Intersection—Returns a new array containing elements common to the two arrays, with no duplicates.

[ 1, 1, 3, 5 ] & [ 1, 2, 3 ]   #=> [ 1, 3 ]
like image 99
Matt Briggs Avatar answered Dec 21 '22 03:12

Matt Briggs