clear persistent model associations for objects in session

I have an order form that is comprised of a lot of :has_many and :has_one associations. I want to load the object into the session, with all associations and edit it.

I want the whole submission process to be done atomically for objects at once. So if I add or edit items on the order, I don't want to be saved to the database until the user clicks submit.

The problem is that I save the order in the session, but the associations get refreshed every time the page reloads. I looked at the code in ction_controller/session_management.rb and clear_persistent_model_associations() takes care of removing associations.

Any suggestions on how to handle this?

Constantin Gavrilescu wrote:

I have an order form that is comprised of a lot of :has_many and :has_one associations. I want to load the object into the session, with all associations and edit it.

I want the whole submission process to be done atomically for objects at once. So if I add or edit items on the order, I don't want to be saved to the database until the user clicks submit.

The problem is that I save the order in the session, but the associations get refreshed every time the page reloads. I looked at the code in ction_controller/session_management.rb and clear_persistent_model_associations() takes care of removing associations.

Any suggestions on how to handle this?

You can serialize and deserialize the whole structure with to_json and from_json, specifying the includes.

But because cookie sessions are limited to 4KB, you should probably store associated ids instead. e.g. create a model attribute that on read converts a has_many to an array of ids, and on write populates the has_many from an array. The association is re-loaded, but you re-create the right set of associates.

Mark Reginald James wrote:

Constantin Gavrilescu wrote:

code in ction_controller/session_management.rb and clear_persistent_model_associations() takes care of removing associations.

Any suggestions on how to handle this?

You can serialize and deserialize the whole structure with to_json and from_json, specifying the includes.

Marshal dump() and load() saves associated objects, too. Is there some specific reason why I should use json?

Maybe I need to explain what I want to achieve. I have an order with a multitude of associated associations which, to further complicate things, have more associations, like extra charges.

Something like: order has many containers container has many extra charges

The user edits the order, adds or deletes containers, adds or deletes extra charges to the containers. The problem is that I want all the changes to be saved the the user presses the "Save order" button, not when he adds or edits each container or extra charge. I wanted to save the whole order and associations in the session until it's ready to save in one atomic operation, but it seems there's no easy way to do this with the way Rails and ActiveRecord works.

Order.find(20).containers.size

=> 1

?> o = Order.find(20) => #<ImportOrder:0x46b3934 @attributes=....

o.containers.size

=> 1

o.containers = Hash.new #This will delete all containers in the database, not just in the memory.

=> {}

Order.find(20).containers.size

=> 0

So when I work with activerecord objects, they might get commited to the database when I don't really want that.

Constantin Gavrilescu wrote:

Maybe I need to explain what I want to achieve. I have an order with a multitude of associated associations which, to further complicate things, have more associations, like extra charges.

Something like: order has many containers container has many extra charges

The user edits the order, adds or deletes containers, adds or deletes extra charges to the containers. The problem is that I want all the changes to be saved the the user presses the "Save order" button, not when he adds or edits each container or extra charge. I wanted to save the whole order and associations in the session until it's ready to save in one atomic operation

I found a way to keep all my order objects into the session until they are ready to be saved: redefine clear_association_cache for class order and it's subclasses.

def clear_association_cache   return if self.class.to_s =~ /Order$/   super end