# Has\_many \<-\> belongs\_to trouble

**URL:** <https://www.ruby-forum.com/t/has-many-belongs-to-trouble/61878>\
**Category:** Rails\
**Created:** [May 29, 2006, 8:15am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-trouble/61878 "2006-05-29T08:15:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [May 29, 2006, 8:15am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-trouble/61878/1 "2006-05-29T08:15:58Z")

</div>

Hi all, finally im starting with rails, i have a simple relationship  
has\_many ans belongs\_to that i dont now how to manage the update action,  
here are my models

class Item \< ActiveRecord::Base  
has\_many :images  
end

class Image \< ActiveRecord::Base  
belongs\_to :item  
end

And my table structure:

Items =\> id, title, desc  
Images =\> id, image, item\_id

ok, i have a number of items and each item has an image assigned, i can  
display the item info and its assigned image with

\<% for image in @item.images %\>  
\<%= image\_tag("/images/publicas/" + image.image) %\>  
\<% end %\>

thats not a problem, the problem comes when i want to update the item, i  
cant find a way to update its image too, im trying to fill a combobox(in  
fact to fill the combo thereÂ´s no problem) in the edit.rhtml view so i  
can choose wich image assign to the item and when i click the edit  
button it be saved on the controller but i cant figure it out how pass  
the image/id parameters.

Hope you can help me with this, Please be gentle im still learning ☹

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [May 29, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-trouble/61878/2 "2006-05-29T20:28:20Z")

</div>

Anyone? i have really tried to achieve this but i just cant figure it  
out how to make it work.

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [May 29, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-trouble/61878/3 "2006-05-29T22:32:54Z")

</div>

Hi Nhila,

I am also learning Rails and are trying to do similar things as you (if  
I understand your situation correctly.) What I did is store the  
parameters I needed in session like

session[:image\_id]

If you find a better way, please let me know 🙂

Victor

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [May 30, 2006, 9:16am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-trouble/61878/4 "2006-05-30T09:16:13Z")

</div>

I should really read what I write before sending:  
“now, in you view, you have params[:image] which contains a hash  
like…”: of  
course, it’s not in your view, but in in your controller.

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [May 30, 2006, 9:13am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-trouble/61878/5 "2006-05-30T09:13:10Z")

</div>

Okay, so if I get it right, you have items, each of them having many  
images.

Now, you simply want to associate the images to each item. You can use  
different controls for that, including the combo box, which I will  
briefy  
describe.

You view could look like that (that’s an image you are editing, and you  
will  
choose which item it belongs to. Editing an item would rather require a  
“check box control”):

\<%= @items= Item.find(:all)  
collection\_select(:image, :item\_id, @items, :id, :item)  
%\>

now, in you view, you have params[:image] which contains a hash like:  
“image”=\>{“name”=\>“my image name”, “item\_id”=\>“12”}  
if you have setup your form as usual, you should also have params[:id] =  
id  
of your image

Therefore, in you controller, you can update like:  
image = Image.find(params[:id])  
if image.update\_attributes(params[:image])  
redirect in case of success  
else  
render form in case of failure  
end

Hope this helps.

Regards,

Nicolas

That should create a combo box where the names of the
