I've been toying over these two problems, but I'm hoping that someone
here might be able to provide a better idea.
1. Every user has their own id (current_user.id) and can add people to a
list with firstname and lastname fields. I understand that
validates_uniqueness_of with :scope should be able to compare at least
one of the fields, but can this validation be done against the combined
first and last name together?
2. Does anyone have a better reference for structuring conditions within
finds? Specifically, I'm trying to figure out how to adjust this query
to find matches between either lastname AND email or lastname and phone
(@contact.phone)
I've been toying over these two problems, but I'm hoping that someone
here might be able to provide a better idea.
1. Every user has their own id (current_user.id) and can add people to a
list with firstname and lastname fields. I understand that
validates_uniqueness_of with :scope should be able to compare at least
one of the fields, but can this validation be done against the combined
first and last name together?
2. Does anyone have a better reference for structuring conditions within
finds? Specifically, I'm trying to figure out how to adjust this query
to find matches between either lastname AND email or lastname and phone
(@contact.phone)
class User
def self.find_by_lastname_etc(lastname, firstname = nil, email = nil)
where_string = "lastname = :lname"
if !firstname.nil?
where_string += " AND firstname = :fname"
end
if !email.nil?
where_string += " AND email = :em"
end
self.find(where_string, {:lname => lastname, :fname => firstname,
:em => email})
end
end