Forum: Ruby on Rails Belongs_to and foreign_id troubles

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
798c7f10aed317c27155ba7c53221df6?d=identicon&s=25 Gustav (Guest)
on 2007-03-03 20:09
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 =

def create
  @image = Image.create!(params[:image])
  @image.item = params[:id]
  redirect_to :action => 'list'

(I've tried switching @image.item to @image.item_id, and params[:id] to

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

<p>Product: <%= %></p>

<% form_for :image, :url => { :action => 'create' }, :html => {
:multipart => true } do |f| -%>

<p><%= f.file_field :uploaded_data %></p>
<p><%= submit_tag :Create %></p>

<% end -%>

My log shows no command to input item_id to be found...

Processing UploadsController#create (for at 2007-03-03
20:05:55) [POST]
  Session ID: fd42ffbe649cc70a002b53acd94926e1
  Parameters: {"commit"=>"Create", "action"=>"create",
  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,
This topic is locked and can not be replied to.