jeudi 19 février 2015

Find a model by a custom property

I have a model which has a custom property:



class User < ActiveRecord
def my_custom_field
# ....
end
end


I need to find a user which has a certain my_custom_field. I can't do this:



User.where(my_custom_field: '123')[0]


However, I can do this:



User.all.select { |x| x == '123'}[0]


but it's not efficient performance-wise. What's a more effective way?


Aucun commentaire:

Enregistrer un commentaire