Issue with validates_uniqueness_of

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

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?