For #2, something like this should work:
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
Cheers,
-Roy
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of
Rob Scott
Sent: Wednesday, July 23, 2008 11:18 AM
To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Subject: [Rails] Beginning find / validation questions
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)
User.find(:first, :conditions => ["lastname = ? AND email = ?",
@contact.lastname, @contact.email])
If anyone could lend a hand, I''d really appreciate it. Sorry if these
questions are a bit basic, as well.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---