Given a simple text string search_for which would be something like “%mike%”, I’d like to find all Users where either “user.email LIKE search_for” or “user_profile.name LIKE search_for”
User has_one UserProfile
User has an :email field
UserProfile has a :name field
user.user_profile might not exist (eg, a user might not have a name yet)
I assume I first need a LEFT JOIN (so I have all users, joined with their user_profile IF it exists), then I need to be able to specify user.email and user_profile.name as part of a .where clause, but I cannot quite crack the syntax to make it work.
Any help would be appreciated.
Found it
@found_all = User.joins(“LEFT OUTER JOIN user_profiles ON user_profiles.user_id = users.id”).where(“(LOWER(users.email) LIKE ?) OR (LOWER(user_profiles.name) LIKE ?)”, srchterm, srchterm)