# Question about saving

**URL:** <https://www.ruby-forum.com/t/question-about-saving/171142>\
**Category:** Rails\
**Created:** [June 26, 2009, 11:46pm UTC](https://www.ruby-forum.com/t/question-about-saving/171142 "2009-06-26T23:46:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [June 26, 2009, 11:46pm UTC](https://www.ruby-forum.com/t/question-about-saving/171142/1 "2009-06-26T23:46:46Z")

</div>

Hi,

I have a couple of classes, defined in simplified form as follows:

class Item \< ActiveRecord::Base  
has\_many :item\_properties, :dependent =\> :delete\_all  
end

class ItemProperty \< ActiveRecord::Base  
belongs\_to :item  
before\_save :clean\_up  
end

When creating a new instance of Item, things work fine - I create an  
Item object, add some ItemProperty objects to it:

item.item\_properties \<\< ItemProperty.new(…)

and save it at the end by calling item.save. Part of validation is  
done in ‘item’, because ItemProperty objects themselves do not have  
enough information to perform full validation (for example, to make  
sure certain ItemProperties do not co-exist in the same Item object).  
When creating, this works fine, because if validation fails, the item  
isn’t created.

When updating, however - and I understand this is by design - things  
don’t work well. If I update an Item object by adding another  
ItemProperty:

item.item\_properties \<\< ItemProperty.new(…)

ItemProperty is immediately saved into the database, even without  
a .save. Note that the contents of Item.item\_properties can vary  
between updates - meaning, changing the ItemProperty object itself,  
instead of rebuilding the list of ItemProperty objects is not an  
option.

To make a long story short, when updating an existing object, I need  
to be able to add as many ItemProperty objects as necessary, and I  
would like no child objects to be saved, until item.save is called -  
because I need the validation in Item to intercept any potential  
issues. Is it possible to delay saving when adding objects to a  
has\_many association, until .save is called (like when creating a new  
object)? If not, what are my options?

I appreciate any help. Thanks

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [June 27, 2009, 2:15am UTC](https://www.ruby-forum.com/t/question-about-saving/171142/2 "2009-06-27T02:15:51Z")

</div>

I achieved the needed result by using collection.build, and using a  
before\_update callback to delete all rows from item\_properties with  
matching item\_id. Still, it feels like a hack, and if anyone has a  
better suggestion, I’d appreciate some feedback. Thanks

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [June 27, 2009, 8:45am UTC](https://www.ruby-forum.com/t/question-about-saving/171142/3 "2009-06-27T08:45:59Z")

</div>

2009/6/26 jack [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â belongs\_to :item  
> enough information to perform full validation (for example, to make  
> sure certain ItemProperties do not co-exist in the same Item object).  
> When creating, this works fine, because if validation fails, the item  
> isn’t created.

Is it not possible to put the validation currently done in Item into  
ItemProperty? You say that the ItemProperty object does not have the  
information, but it does have access to its parent Item so you may be  
able to access this and fail the validation if appropriate. This  
would prevent the save.

> option.  
> I don’t understand this point.

Colin
