# Select i a partial

**URL:** <https://www.ruby-forum.com/t/select-i-a-partial/105207>\
**Category:** Rails\
**Created:** [May 27, 2007, 6:24am UTC](https://www.ruby-forum.com/t/select-i-a-partial/105207 "2007-05-27T06:24:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [May 27, 2007, 6:24am UTC](https://www.ruby-forum.com/t/select-i-a-partial/105207/1 "2007-05-27T06:24:17Z")

</div>

Hello i got this partial  
\_form

\<%= error\_messages\_for :product %\>

Name  
\<%= text\_field 'product', 'name' %\>

Description  
\<%= text\_area 'product', 'description' %\>

Category  
\<%= select(:id,'category',@categories) %\>

in the model i got  
class Product \< ActiveRecord::Base  
belongs\_to :category  
validates\_presence\_of :name

end

the problem is that i dont see where is the problem to update the cell  
“category\_id” \<\<\< it always in ull

here is my controller

def update  
@product = Product.find(params[:id])  
if @product.update\_attributes(params[:product])  
flash[:notice] = ‘Product ready.’  
redirect\_to :action =\> ‘show’, :id =\> @product  
else  
render :action =\> ‘edit’  
end  
end

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [May 27, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/select-i-a-partial/105207/2 "2007-05-27T23:28:09Z")

</div>

I thought when you put “belongs\_to” in model, it save the category id  
automatically in the db.

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [May 27, 2007, 9:40am UTC](https://www.ruby-forum.com/t/select-i-a-partial/105207/3 "2007-05-27T09:40:00Z")

</div>

Edgar G. wrote:

> Hello i got this partial  
> \_form
> 
> \<%= error\_messages\_for :product %\>
> 
> Name  
> \<%= text\_field 'product', 'name' %\>
> 
> Description  
> \<%= text\_area 'product', 'description' %\>
> 
> Category  
> \<%= select(:id,'category',@categories) %\>
> 
> in the model i got  
> class Product \< ActiveRecord::Base  
> belongs\_to :category  
> validates\_presence\_of :name
> 
> end

you have to assign the category to the product (the product belongs\_to  
category).  
as i gather from peaking at your post, the selected select-tag that you  
have there (selecting from @categories) should be the category\_id of the  
@product.  
so, you have to assign it. this can be done in many different ways. here  
are two solutions:

1. 

change the select(:id, ‘category’, @categories) to select(:category\_id,  
‘product’, @categories) # this changes to the correct params, and will  
update the correct column (category\_id)

1. 

in the controller add:  
@product.category\_id = params[:id] #if the select tag has select(:id  
… which means the params[:id] is the value of what was chosen in the  
select\_tag

hth.

–shai

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [May 27, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/select-i-a-partial/105207/4 "2007-05-27T23:59:50Z")

</div>

Wel i got this resolved doing this:

Categoria  
\<%= select(:producto,'categoria\_id',@categorias) %\>

:product = the model i am using  
category\_id = the field i want to fill  
@categories = the choices

thanks for all Shai  
Edgar G. wrote:

> I thought when you put “belongs\_to” in model, it save the category id  
> automatically in the db.
