Let’s say we have 3 users [User name="A"], [User name="B"], [User name="C"]
Is there a difference between (to check presence):
if User.where(name: "A").first
and
User.all.map(&:name).include? "A"
Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using
whereonly retrieves users who match the name, via SQL.Mapping loads all the users, builds an array of their names, and checks for ‘A’.
For small numbers of users the difference in performance will be negligible, but both memory and time will be substantially impacted after a certain point, and
wherewill be much better. Doing the work in the DB will be a better option than naively retrieving all data and doing it in Ruby.