Hi, Check the following data model: class BaseClass < ActiveRecord::Base; end class Class1 < BaseClass; end class Class2 < BaseClass; end For some reasons, I would like to "convert" a Class1 object to a Class2 object. Creating a new Class2 object from Class1 object is not a solution, since Class1 object is associated to lots of objects and I do not want to recreate the associations. obj = BaseClass.find(<id>) => how to cast obj from Class1 to Class2 Thank you! Jean-Etienne -- 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 -~----------~----~----~----~------~----~------~--~---
One way is to manually set the type column: obj[:type] = Class2 obj.save Chris On May 2, 12:51 pm, Jean-Etienne Durand <rails-mailing-l...@andreas- s.net> wrote:> Hi, > > Check the following data model: > class BaseClass < ActiveRecord::Base; end > class Class1 < BaseClass; end > class Class2 < BaseClass; end > > For some reasons, I would like to "convert" a Class1 object to a Class2 > object. > Creating a new Class2 object from Class1 object is not a solution, since > Class1 object is associated to lots of objects and I do not want to > recreate the associations. > > obj = BaseClass.find(<id>) > => how to cast obj from Class1 to Class2 > > Thank you! > Jean-Etienne > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
You might need to use update_attribute to avoid validation checking on save... BaseClass.find(<id>).update_attribute(:type, Class1) On May 2, 8:15 am, Chris Mear <c...-OIzkuoyqg0kAvxtiuMwx3w@public.gmane.org> wrote:> One way is to manually set the type column: > > obj[:type] = Class2 > obj.save > > Chris > > On May 2, 12:51 pm, Jean-Etienne Durand <rails-mailing-l...@andreas- > > s.net> wrote: > > Hi, > > > Check the following data model: > > class BaseClass < ActiveRecord::Base; end > > class Class1 < BaseClass; end > > class Class2 < BaseClass; end > > > For some reasons, I would like to "convert" a Class1 object to a Class2 > > object. > > Creating a new Class2 object from Class1 object is not a solution, since > > Class1 object is associated to lots of objects and I do not want to > > recreate the associations. > > > obj = BaseClass.find(<id>) > > => how to cast obj from Class1 to Class2 > > > Thank you! > > Jean-Etienne > > > -- > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---