# Beginning find / validation questions

**URL:** <https://www.ruby-forum.com/t/beginning-find-validation-questions/143914>\
**Category:** Rails\
**Created:** [July 23, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/beginning-find-validation-questions/143914 "2008-07-23T20:17:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [July 23, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/beginning-find-validation-questions/143914/1 "2008-07-23T20:17:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [July 23, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/beginning-find-validation-questions/143914/2 "2008-07-23T23:24:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [July 23, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/beginning-find-validation-questions/143914/3 "2008-07-23T23:47:49Z")

</div>

On Wed, Jul 23, 2008 at 1:17 PM, Rob Scott  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

Not using validates\_uniqueness\_of. You can however write your own  
validation function using validates\_each. See:  
[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M001045](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M001045)

> 1. 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])

You can just use a standard sql where fragment:

User.find(:first, :conditions =\> [“(lastname = ? AND email = ?) OR  
(lastname = ? and phone = ?)”, @contact.lastname, @contact.email,  
@contact.lastname, @contact.phone])
