Belonging models not saving individually

Hi, I have some Page model that has_many Photos, I am using Paperclip for doing the file processing for the photos and I am following the Ryan Bates (Advanced Rails Recipes) recipe for handling multiple belonging models and parent model in a single form (wich i implemented into a gem) so I can dynamically add and remove photos.

Ok, the problem is when creating a Page and validation fails: the user has to upload all the files again. I thought of saving the photos without validation when creating a Page so Paperclip does it's job, I would have a bg task to delete orphan Photos:

class Page < ActiveRecord::Base   accepts_multiple :photos, :dependent => :destroy #this method comes from my                                     gem implementation of the Ryan B. recipe

  validates_associated :photos   validates_presence_of :title, :content

  after_validation_on_create :save_attachments

  def save_attachments     unless self.errors.empty?       self.photos.each{ |photo| photo.save( false ) }     end   end end

The problem is that the Photos don't get saved in the database although they return false for #new_record? and Paperclip saves the files, if I set a debugger here:

  def create     @page = Page.new(params[:page])

    if @page.save       ...     else       debugger       render :action => "new"     end   end

and play with the irb:

@planta.fotos.first

=> #<Photo id: 1, page_id: nil...

@planta.fotos.first.new_record?

=> false

Photo.count

=> 0

Photo.find( 1 )

=> ActiveRecord::RecordNotFound Exception: Couldn't find Photo with ID=1

@planta.fotos.first.save( false )

=> true

Photo.count

=> 0

This is most annoying and strange, it should be saved!!!

In the log it appears as it has ben written to db: Photo Create (0.8ms) INSERT INTO "photos" ("updated_at"...

Is there any kind of transaction or caching involved? Can I override this behavior? Please help!!

Macario Ortega wrote:

The problem is that the Photos don't get saved in the database although they return false for #new_record? and Paperclip saves the files, if I set a debugger here:

  def create     @page = Page.new(params[:page])

    if @page.save       ...     else       debugger       render :action => "new"     end   end

and play with the irb:

@planta.fotos.first

=> #<Photo id: 1, page_id: nil...

@planta.fotos.first.new_record?

=> false

Photo.count

=> 0

Photo.find( 1 )

=> ActiveRecord::RecordNotFound Exception: Couldn't find Photo with ID=1

@planta.fotos.first.save( false )

=> true

Photo.count

=> 0

This is most annoying and strange, it should be saved!!!

In the log it appears as it has ben written to db: Photo Create (0.8ms) INSERT INTO "photos" ("updated_at"...

Is there any kind of transaction or caching involved? Can I override this behavior?

Rails automatically wraps saves in transactions, but does not reset the ids of successful saves within a rolled-back save, even though those saves will themselves be rolled-back.

You'll have to do something like

@page.save_without_transactions

or save your photos outside the page save.