Juanma Cervera
2008-Sep-14 08:01 UTC
Serializing an AR object into an attribute in another
Hi, I don''t know if this is a bug or it is the expected behavior but I am trying to serialize an AR object into another. I know this is not very usual but I don''t want to use a belongs_to association because the object is transient. I need an snapshot of how the 2nd object was at the moment of the creation of the 1st. class One < ActiveRecord::Base end class Two < ActiveRecord::Base serialize one end obj1 = One.find(:first) obj2 = Two.create(...) obj2.one = obj1 obj2.save My problem is that it seems that AectiveRecord only saves the id of the serialized object when create or update the second object and not all the yaml format of the serialized. There is not any association between the two objects. Anyone knows if I am on the right track? Or I need to change the direction? Thanks Juanma Cervera -- 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 -~----------~----~----~----~------~----~------~--~---
DyingToLearn
2008-Sep-14 09:30 UTC
Re: Serializing an AR object into an attribute in another
You might want to try doing your own "serializing". class Two < ActiveRecord::Base def one=(new_one) self.write_attribute(:one, new_one.to_yaml) end def one @one ||= YAML.load(self.read_attribute(:one)) end end I haven''t tested this, but it might be a place to start. Juanma Cervera wrote:> Hi, > > I don''t know if this is a bug or it is the expected behavior but I am > trying to serialize an AR object into another. > I know this is not very usual but I don''t want to use a belongs_to > association because the object is transient. > I need an snapshot of how the 2nd object was at the moment of the > creation of the 1st. > > class One < ActiveRecord::Base > end > > class Two < ActiveRecord::Base > serialize one > end > > obj1 = One.find(:first) > obj2 = Two.create(...) > obj2.one = obj1 > obj2.save > > My problem is that it seems that AectiveRecord only saves the id of the > serialized object when create or update the second object and not all > the yaml format of the serialized. > There is not any association between the two objects. > > Anyone knows if I am on the right track? Or I need to change the > direction? > > Thanks > Juanma Cervera > -- > 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 -~----------~----~----~----~------~----~------~--~---
Juanma Cervera
2008-Sep-14 17:08 UTC
Re: Serializing an AR object into an attribute in another
It has been easy with your help. Many thanks. -- 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 -~----------~----~----~----~------~----~------~--~---