# Validates\_uniqueness\_of missing in rails 3.0 rc2?

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-missing-in-rails-3-0-rc2/194042>\
**Category:** Rails\
**Created:** [August 28, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-missing-in-rails-3-0-rc2/194042 "2010-08-28T21:37:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@anthonys](https://www.ruby-forum.com/u/anthonys)\
**Post date:** [August 28, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-missing-in-rails-3-0-rc2/194042/1 "2010-08-28T21:37:40Z")

</div>

Has anybody noticed validates\_uniqueness\_of missing in rails 3.0 rc2?  
I’m not sure if this is a bug or I’m just doing something wrong.

Here’s what I’m doing.

Class 1:

class Title \< ActiveRecord::Base  
validates\_length\_of :language, :is =\> 3  
validates\_uniqueness\_of :language, :scope =\> :book\_id  
validate :language, :is\_proper\_language\_code

validates\_presence\_of :value

belongs\_to :book

def is\_proper\_language\_code  
errors.add(:base, “invalid language code”) unless  
!ISO\_639::ISO\_639\_2.find{ |lang| lang[0] == self.language }.nil?  
end  
end

Class 2:

class Book \< ActiveRecord::Base  
attr\_accessible :titles

validates\_presence\_of :titles

has\_many :titles  
end

Spec:

before(:each) do  
@valid\_title\_attributes = {:book\_id =\> 1, :language =\> “eng”, :value  
=\> “…”}

```
@book = Book.new
@book.titles.push(Title.new(@valid_title_attributes))

```

end

it “should not have multiple titles with same language attribute” do  
@book.titles.clear  
@book.titles.push(Title.new(@valid\_title\_attributes))  
@book.titles.push(Title.new(@valid\_title\_attributes))  
@book.should\_not be\_valid

```
  @book.titles.clear
  @book.titles.push(Title.new(@valid_title_attributes))
  @book.should be_valid

```

end

---

<div class="post-metadata">

**Author:** ![anthonys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@anthonys](https://www.ruby-forum.com/u/anthonys)\
**Post date:** [August 28, 2010, 11:13pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-missing-in-rails-3-0-rc2/194042/2 "2010-08-28T23:13:39Z")

</div>

On 28 Aug 2010, at 20:32, Anthony S. wrote:

> Has anybody noticed validates\_uniqueness\_of missing in rails 3.0 rc2? I’m not sure if this is a bug or I’m just doing something wrong.

It’s still in there, as far as I can see. If it had been removed, you’d  
get an error when you try to call ‘validates\_uniqueness\_of’.

> attr\_accessible :titles
> 
> ```
> @book.titles.clear
> @book.titles.push(Title.new(@valid_title_attributes))
> @book.should be_valid
> 
> ```
> 
> end

I assume the problem is that “@book.should\_not be\_valid” is failing? The  
uniqueness validator works by querying the database for other matching  
records. So if you’re just creating titles in memory, and not saving  
them, the uniqueness validator won’t complain about them. Try changing  
your 'Title.new’s to ‘Title.create’.

Chris
