FileColumn question

Can someone please help me with this.

I have an Images table :-

create table images (
id integer unsigned not null auto_increment primary key,
image varchar(200) not null default ‘’
)ENGINE=InnoDB default CHARSET=latin1;

And a car table :-
create table cars (
id integer unsigned not null auto_increment primary key,
user_id integer unsigned not null,
title varchar(100) not null,
image_id integer unsigned not null,
constraint fk_cars_user foreign key (user_id) references
users(id),
constraint fk_cars_image foreign key (image_id) references images(id)
)ENGINE=InnoDB default CHARSET=latin1;

( The user table I’m using is the one provided by the Salted Login
Generator ).

Now I want my Cars Table to have many images.

  1. How should I modify FileColumn to upload…and view the images?
  2. How could I modify FileColum to allow for users to delete images?

Thanks in advance.

Hi!

First of all i’m a newbie myself, so there may be some errors in this
code. I had the same problem lately and i solved it thanks to people on
this forum.

If you want your cars to have many images than you have to change your
tables a bit: now you have image_id foreign key in cars table. This way
if you want to have a car with 4 images you’d have to have 4 rows with
the same car data and just different image_id.

You should remove this foreign key and create new ‘car_id’ in images
table.
This way every row in images table will specify a single image. If you
have many images with the same car_id they will all belong to one car,
so this car will have many images.

Then you need to specify in your Car model:
has_many :images, :dependent => true

and in your Image model:
belongs_to :car
file_column :image

Now if you want to upload many images in a single form you should
(probably) change this line in file_column source code. If you will
upload just one image at a time, you don’t need to do this.

In file_column_helper.rb change in the first method:

result = ActionView::Helpers::InstanceTag.new(object.dup,
method.to_s+"_temp", self).to_input_field_tag(“hidden”, {})

to this:

result = ActionView::Helpers::InstanceTag.new(object.dup,
method.to_s+"_temp", self).to_input_field_tag(“hidden”, options)

Now you can create many file_column_fields like this:
<% 5.times do |i| %>
<% @image = @images[i] %>
<%= file_column_field “image”, “name”, “index” => i %>
<% end %>

And to display them later:
<% @car.images.each do |image| %>
<%= image_tag url_for_file_column( “image”, “image”) %>
<% end %>

If you don’t know how to create code for your car controller, look for
the topic “How to save parent and child objects in a single action”.