tpw
tpw

Reputation: 2829

Rails: look up model by multiple attributes

I have a user model with attributes firstname and lastname. Instead of looking up users by id, is there a way I can look them up by firstname lastname pairs? There can be duplicate first names and duplicate last names; however, I don't allow for there to be duplicate firstname-lastname pairs.

Thanks.

Upvotes: 0

Views: 57

Answers (1)

knrz
knrz

Reputation: 1811

If you're using Rails 4: User.find_by(firstname: "Joe", lastname: "Smith")

Otherwise: User.where(firstname: "Joe", lastname: "Smith").first()

Upvotes: 4

Related Questions