# Issue with validates\_uniqueness\_of

**URL:** <https://www.ruby-forum.com/t/issue-with-validates-uniqueness-of/138440>\
**Category:** Rails\
**Created:** [May 23, 2008, 2:05am UTC](https://www.ruby-forum.com/t/issue-with-validates-uniqueness-of/138440 "2008-05-23T02:05:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 23, 2008, 2:05am UTC](https://www.ruby-forum.com/t/issue-with-validates-uniqueness-of/138440/1 "2008-05-23T02:05:58Z")

</div>

so heres some of my code:

class Item \< ActiveRecord::Base  
belongs\_to :item\_type  
validates\_uniqueness\_of :name  
…

the issue i think i’m having is that I have another table which also has  
a column ‘name’ which i am using a collection select from in a form for  
Items:

\<% form\_for(@item) do |f| %\>  
\<%= f.text\_field :name %\>  
\<%= f.collection\_select(:item\_type\_id, @item\_types, :id, :name) %\>  
…  
\<% end %\>

whenever i try to create a new object i get:

error in Items#create  
You have a nil object when you didn’t expect it!

at \<%= f.collection\_select(:item\_type\_id, @item\_types, :id, :name) %\>

i’m not really sure what is going on here or why it is getting an error  
for a form on new.html.erb but it mentions Items#create at the top.

the form works fine if i take out validates\_uniqueness\_of

anyone have a clue what is going on here?

do i need to explicitly state that the :name in the collection select is  
from ‘item\_type.name’?

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 23, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/issue-with-validates-uniqueness-of/138440/2 "2008-05-23T15:40:21Z")

</div>

Scott K. wrote:

> so heres some of my code:
> 
> class Item \< ActiveRecord::Base  
> belongs\_to :item\_type  
> validates\_uniqueness\_of :name  
> …
> 
> the issue i think i’m having is that I have another table which also has  
> a column ‘name’ which i am using a collection select from in a form for  
> Items:
> 
> \<% form\_for(@item) do |f| %\>  
> \<%= f.text\_field :name %\>  
> \<%= f.collection\_select(:item\_type\_id, @item\_types, :id, :name) %\>  
> …  
> \<% end %\>
> 
> whenever i try to create a new object i get:
> 
> error in Items#create  
> You have a nil object when you didn’t expect it!
> 
> at \<%= f.collection\_select(:item\_type\_id, @item\_types, :id, :name) %\>
> 
> i’m not really sure what is going on here or why it is getting an error  
> for a form on new.html.erb but it mentions Items#create at the top.
> 
> the form works fine if i take out validates\_uniqueness\_of
> 
> anyone have a clue what is going on here?
> 
> do i need to explicitly state that the :name in the collection select is  
> from ‘item\_type.name’?

well after commenting out the collection select for @item\_types it gets  
an error on the next collection select:

\<%= f.select(:manufacturable, { “Yes” =\> “Yes”, “No” =\> “No”}) %\>

after some more testing it look like the validates uniqueness works  
properly if i have all my collection select boxes commented out.

So…the issue is why are my collection select boxes coming back with nil  
values after it fails on validation and returns to new Item form?
