# Newbie validation question

**URL:** <https://www.ruby-forum.com/t/newbie-validation-question/98828>\
**Category:** Rails\
**Created:** [July 18, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/newbie-validation-question/98828 "2006-07-18T14:41:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [July 18, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/newbie-validation-question/98828/1 "2006-07-18T14:41:37Z")

</div>

I am having problems with a validation, the full model code is below.

The validate routine is attempting to see if another record with the  
same artist\_name, title, & catalogue\_no exists in the products table. If  
so, it should throw an error.

This is the validation code:

def validate  
if Product.find(:all, :conditions =\> [“artist\_name = ?, title = ?,  
catalogue\_no = ?”, artist\_name, title, catalogue\_no])  
errors.add(:title, "This title already exists, artist: " +  
artist\_name.to\_s + ", title: " + title.to\_s + ", catalogue number: " +  
catalogue\_no)  
end

When I use Product.find I receive this error:

Mysql::Error: #21000Operand should contain 1 column(s): SELECT \* FROM  
products WHERE (artist\_name = ‘Jimmie Rodgers’, title = ‘Rodgers’,  
catalogue\_no = ‘38134’)

I have tried self.find & I receive this error:  
undefined method `find’ for #Product:0x38137d8

I think I understand why both occur, but I am now at a loss as how to  
achieve my desired functionality.

Any help would be greatly appreciates, also it would be great to have  
some outline to me why Product.find & self.find bring throw errors.

rgds,

- matt

* * *

* * *

class Product \< ActiveRecord::Base  
belongs\_to :label  
has\_many :tracks

```
validates_presence_of :country, :title, :catalogue_no, :release_date, 

```

:format, :barcode, :disc\_no, :genre\_main

```
validates_uniqueness_of :catalogue_no

def validate
  if Product.find(:all, :conditions => ["artist_name = ?, title = ?, 

```

catalogue\_no = ?", artist\_name, title, catalogue\_no])  
errors.add(:title, "This title already exists, artist: " +  
artist\_name.to\_s + ", title: " + title.to\_s + ", catalogue number: " +  
catalogue\_no)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [July 18, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/newbie-validation-question/98828/2 "2006-07-18T14:43:54Z")

</div>

Matt,

Valid SQL statement is:

Product.find(:all, :conditions =\> [“artist\_name = ? AND title = ? AND  
catalogue\_no = ?”, artist\_name, title, catalogue\_no])

Jean-Etienne

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [July 18, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/newbie-validation-question/98828/3 "2006-07-18T14:50:16Z")

</div>

Jean-Etienne wrote:

> Matt,
> 
> Valid SQL statement is:
> 
> Product.find(:all, :conditions =\> [“artist\_name = ? AND title = ? AND  
> catalogue\_no = ?”, artist\_name, title, catalogue\_no])
> 
> Jean-Etienne

Many Thanks, all works fine now - your response time was nothing short  
of amazing.

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [July 19, 2006, 12:43am UTC](https://www.ruby-forum.com/t/newbie-validation-question/98828/4 "2006-07-19T00:43:45Z")

</div>

Matt S. wrote:

> catalogue\_no = ?", artist\_name, title, catalogue\_no])  
> errors.add(:title, "This title already exists, artist: " +  
> artist\_name.to\_s + ", title: " + title.to\_s + ", catalogue number: " +  
> catalogue\_no)  
> end

Take a look at the documentation for validates\_uniqueness\_of

[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000816](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000816)

This might work (in Product):

validates\_uniqueness\_of :catalogue\_no,  
:scope =\> [:artist\_name, :title]

regards

Justin
