# Belongs\_to and foreign\_id troubles

**URL:** <https://www.ruby-forum.com/t/belongs-to-and-foreign-id-troubles/87553>\
**Category:** Rails\
**Created:** [March 3, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/belongs-to-and-foreign-id-troubles/87553 "2007-03-03T20:09:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 3, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/belongs-to-and-foreign-id-troubles/87553/1 "2007-03-03T20:09:19Z")

</div>

Hi everyone,  
I’m trying to connect an uploaded image (using act\_as\_attachment) to an  
item in my database. Uploading the image in itself is no problem, though  
I can’t seem to get the foreign-id to “stick”.

I’ve set up my models (image.rb and item.rb). In image.rb I’ve setup the  
“belongs\_to :item”-association and created an “item\_id” in my  
images-table using migrations (and I’ve confirmed it has been in fact  
created in YourSQL).

In my uploads-controller I’ve setup the following:

def new  
@item = Item.find(params[:id])  
@image = Image.new  
end

def create  
@image = Image.create!(params[:image])  
@image.item = params[:id]  
redirect\_to :action =\> ‘list’  
end

(I’ve tried switching @image.item to @image.item\_id, and params[:id] to  
params[:item])

To upload a new image I enter the URL: “/uploads/new/12”. My  
uploads-view looks like this:

Product: \<%= @item.name %\>

\<% form\_for :image, :url =\> { :action =\> ‘create’ }, :html =\> {  
:multipart =\> true } do |f| -%\>

\<%= f.file\_field :uploaded\_data %\>

\<%= submit\_tag :Create %\>

\<% end -%\>

My log shows no command to input item\_id to be found…

Processing UploadsController#create (for 127.0.0.1 at 2007-03-03  
20:05:55) [POST]  
Session ID: fd42ffbe649cc70a002b53acd94926e1  
Parameters: {“commit”=\>“Create”, “action”=\>“create”,  
“controller”=\>“uploads”,  
“image”=\>{“uploaded\_data”=\>#\<File:/tmp/CGI.830.1\>}}  
Image Columns (0.037518) SHOW FIELDS FROM images  
SQL (0.002857) BEGIN  
SQL (0.000795) INSERT INTO images (`content_type`, `item_id`,  
`size`, `thumbnail`, `db_file_id`, `filename`, `height`, `parent_id`,  
`width`) VALUES(‘image/jpeg’, NULL, 168427, NULL, NULL, ‘DSC00414.jpg’,  
428, NULL, 640)  
SQL (0.029149) COMMIT

Thanks for any help!

Best regards,  
Gustav
