lundi 22 juin 2015

String concatenation of two attributes in Rails where query

Let's say I have the attrs first_name and last_name for a User. I know the user's full name is "Johnny Appleseed", as I have a large Hash of user's full names as strings.

Sure, I could split each string first, and search like so: User.where(first_name: 'Johnny', last_name: "Appleseed")

BUT, I'm wondering if there is a way to basically concat the two in a query, essentially like so: User.where('first_name + last_name = ?', 'Johnny Appleseed')

Or if I could have a full_name method on the User model, and search by that?

 def full_name
    "#{self.first_name} #{self.last_name}"
  end

User.where('full_name = ?', 'Johnny Appleseed')

Aucun commentaire:

Enregistrer un commentaire