Constantin Gavrilescu
2008-Feb-03 13:02 UTC
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Feb-03 14:00 UTC
Re: clear persistent model associations for objects in session
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. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.> 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. > > -- > We develop, watch us RoR, in numbers too big to ignore.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 operationI 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This seems to address the same problem that i''m trying to find a suitable workaround for. I''ve tried your solution (redefining the clear_association_cache method on the parent object) and on first try it looks to be achieving the desired effect of maintaining the child data in the session. I was just wondering if this solution ended up working for you long-term, or if any undesirable side effects appeared? Thanks very much for your time, jesse -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---