willfore
willfore

Reputation: 170

Searching Hash for entry that does not contain value

I am not quite sure how to title this question and I am having a hard time getting it to work so here goes.

I have a hash of users which is various sizes. This can be anywhere from 2 to 40. I also have a hash of tickets which I want to search through and find any entries that do not contain the user_id of my users hash. I am not quite sure how to accomplish this. My last attempt I used this:

@not_found = []
users.each do |u|
  @not_found += @tickets.select{|t| t["user_id"] != u.user_id}
end

I know this is not the right result as it does a comparison for only the one user_id. What I need to do is run through all of the tickets and pull out any results that contain a user_id that is not in the users hash.

I hope I am explaining this properly and appreciate any help!

Upvotes: 0

Views: 52

Answers (1)

Wand Maker
Wand Maker

Reputation: 18762

Try this

known_user_ids = users.map(&:user_id)
tickets_with_unknown_users = @tickets.reject{ |t| known_user_ids.include?(t["user_id"]) }

Upvotes: 1

Related Questions