Question about saving

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

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

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.

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.

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.

I don't understand this point.

Colin