Topic: attachment_fu - update image leaves old database records
Hey everyone,
I've successfully implemented picture uploads to my application using attachment_fu. I have two models - a 'product' model and a 'picture' model. The 'picture' model belongs to the 'product' model, and this is where the relationship between tables is made, and it's working fine when I upload a picture. However, when I change a picture from a product that already had a picture related to it (using a regular 'update' method), it updates the picture successfully. But it leaves the old database records from the previous picture in the database. I obviously don't want thousands of stale database records in my database.
I don't know if this is the proper way of updating a picture using attachment_fu. Every tutorial I've found out there just tells you how to add attachment_fu and how to upload a picture for the first time, nothing else. Here's my 'update' method in the products controller:
def update
@product = Product.find(params[:id])
if !params[:picture].empty?
@product.picture = Picture.new(params[:picture])
end
if @product.update_attributes(params[:product])
flash[:notice] = "Product updated!"
redirect_to product_path(@product)
else
render :action => "edit"
end
end
I would really appreciate any help on this. Thanks!