views:

38

answers:

2

Hi all.

When uploading photos I got inconsistent rollbacks in the log file. I selected the same picture over and over again until the system accepted it, after the 3rd to 12th try.

This stopped after I added the photo.valid? before photo.save.

My code uses attachment_fu, below you can see the controller code:

@album = @listing.album 
@p1 = params[:p1]    
if @p1[:uploaded_data] != ""
    @photo = @album.photos.build(params[:p1])
    @photo.valid?        
    if @photo.save   
      @album.photos << @photo                                                    
    end
end

So whats wrong with my code here? Why do I need the valid?

Thank you in advance,

Michael

Edit: The log file without the valid? gives a rollback:

[4;35;1mAlbum Load (0.0ms)[0m   [0mSELECT * FROM `albums` WHERE (`albums`.listing_id = 68) LIMIT 1[0m
[4;36;1mAlbum Columns (0.0ms)[0m   [0;1mSHOW FIELDS FROM `albums`[0m
[4;35;1mPhoto Columns (0.0ms)[0m   [0mSHOW FIELDS FROM `photos`[0m
[4;36;1mSQL (0.0ms)[0m   [0;1mBEGIN[0m
[4;35;1mSQL (0.0ms)[0m   [0mROLLBACK[0m
User did not upload a picture.
Rendering template within layouts/application
Rendering listings/images

Edit: This can be solved using Omars tip:

logger.error @photo.errors.inspect

I get @errors={"size"=>["is not included in the list"] which is the source of the problem.

A: 

Can you show us the log file? AFAIK .save validates the model ... twice.

Omar Qureshi
A: 

Change to

if @photo.save   
  @album.photos << @photo                                                    
else
  logger.error @photo.errors.inspect
end
Omar Qureshi
what was the answer?
Omar Qureshi
ah, sorry, ignore, just re-read question edit
Omar Qureshi