# Belonging models not saving individually

**URL:** <https://www.ruby-forum.com/t/belonging-models-not-saving-individually/158581>\
**Category:** Rails\
**Created:** [January 25, 2009, 11:40am UTC](https://www.ruby-forum.com/t/belonging-models-not-saving-individually/158581 "2009-01-25T11:40:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![macario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@macario](https://www.ruby-forum.com/u/macario)\
**Post date:** [January 25, 2009, 11:40am UTC](https://www.ruby-forum.com/t/belonging-models-not-saving-individually/158581/1 "2009-01-25T11:40:00Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![macario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@macario](https://www.ruby-forum.com/u/macario)\
**Post date:** [January 26, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/belonging-models-not-saving-individually/158581/2 "2009-01-26T16:42:57Z")

</div>

Macario O. wrote:

> ```
> debugger
> 
> ```
> 
> > > Photo.count
> 
> 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.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
