Array#reject again to the rescue! Like Array#map , it accepts blocks, allowing you to do something like this:
zip_codes = CardSignup.all.map(&:zip_code) @email.distributions.reject{|o| !zip_codes.include?(o.zip_code)}
Oh, but of course, if you like finding more elegant ways, always consider operators like you. & will return a new array with objects in both, | will connect and remove duplicates.
ruby-1.9.2-p0 > [1,2] | [2,3] => [1, 2, 3] ruby-1.9.2-p0 > [1,2] & [2,3] => [2]
Edit: as Tokland said in the comments, since this applies to the Rails model, you might want to do this as a choice. Like this -
zip_codes = CardSignup.all.map(&:zip_code) @email.distributions.where('zip_code IN (?)', zip_codes)
Or do it with an INNER JOIN. Doesn't look beautiful though.
@email.distributions.joins('INNER JOIN card_signups ON card_signups.zip_code = email_distributions.zip_code').all
(If the table for @ email.distributions is email_distributions ..)
source share