# Verification problems

**URL:** <https://www.ruby-forum.com/t/verification-problems/51296>\
**Category:** Rails\
**Created:** [December 29, 2005, 6:55am UTC](https://www.ruby-forum.com/t/verification-problems/51296 "2005-12-29T06:55:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [December 29, 2005, 6:55am UTC](https://www.ruby-forum.com/t/verification-problems/51296/1 "2005-12-29T06:55:49Z")

</div>

Hello,

Hope this isn’t a newbie question - but I don’t seem to be able to find  
relevent information elsewhere.

# I am having trouble getting the following to work: Schema

ActiveRecord::Schema.define(:version =\> 1) do

create\_table “authors”, :force =\> true do |t|  
t.column “name”, :string  
end

create\_table “books”, :force =\> true do |t|  
t.column “title”, :string  
t.column “author\_id”, :integer  
end

create\_table “catalogs”, :force =\> true do |t|  
t.column “author\_id”, :integer  
t.column “book\_id”, :integer  
end

# Models

class Author \< ActiveRecord::Base  
has\_many :book

validates\_uniqueness\_of :name  
end  
class Book \< ActiveRecord::Base  
belongs\_to :author  
has\_many :catalog

validates\_presence\_of :author, :message =\> “doesn’t exist!”  
validates\_associated :author

# Each book title must be unique to each author.

def validate  
if Book.find\_by\_title\_and\_author\_id(self.title, self.author\_id)  
errors.add(:title, “(#{self.title}) already exists for this author  
(#{self.author.name})”)  
end  
end  
end  
class Catalog \< ActiveRecord::Base  
belongs\_to :book

validates\_presence\_of :book  
validates\_associated :book  
end

NOTE: The above is a contrived example to simplify the problem (in  
reality the Catalogs table has other data associated with it).

The problem is that you can add authors and books, and all the  
validation works like a charm (including checking the uniqueness across  
multiple columns for a book), but when I try to add something to the  
catalog, it is trying to validate the book model again and fails on the  
Book.find\_by\_title\_and\_author\_id() as the book obviously already exists.

Why is rails trying to revalidate the Book model when I am adding  
something to the Catalog (it isn’t a save or update to Book is it?), am  
I doing something wrong by checking uniqueness across multiple columns  
in the model (if I remove it all works, but then I end up with duplicate  
books).

Regards

Glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [December 29, 2005, 2:02pm UTC](https://www.ruby-forum.com/t/verification-problems/51296/2 "2005-12-29T14:02:37Z")

</div>

Glenn wrote:

> books).  
> Perhaps you want either:

```
   validates_associated :book, :on => :create

```

## or def validate\_on\_create …

We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [December 29, 2005, 8:30pm UTC](https://www.ruby-forum.com/t/verification-problems/51296/3 "2005-12-29T20:30:28Z")

</div>

> # Each book title must be unique to each author.
> 
> def validate  
> if Book.find\_by\_title\_and\_author\_id(self.title, self.author\_id)  
> errors.add(:title, “(#{self.title}) already exists for this author  
> (#{self.author.name})”)  
> end  
> end

This can be written as  
validates\_uniqueness\_of :title, :scope =\> ‘author\_id’

See  
[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685)  
for details.

-Ben

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [December 29, 2005, 11:34pm UTC](https://www.ruby-forum.com/t/verification-problems/51296/4 "2005-12-29T23:34:31Z")

</div>

Benjamin S. wrote:

> > # Each book title must be unique to each author.
> > 
> > def validate  
> > if Book.find\_by\_title\_and\_author\_id(self.title, self.author\_id)  
> > errors.add(:title, “(#{self.title}) already exists for this author  
> > (#{self.author.name})”)  
> > end  
> > end
> 
> This can be written as  
> validates\_uniqueness\_of :title, :scope =\> ‘author\_id’
> 
> See  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685)  
> for details.
> 
> -Ben

Perfect - it works. Thanks for the tip.

Regards

Glenn
