I have some models which use the same table via single table inheritance. I (think I) need to change objects from one class to another and not sure how to go about that. Consider these models: class Project < ActiveRecord::Base end class Goal < Project # inherits project characteristics end class Assignment < Project # inherits project characteristics end They are distinguished from each other by the ''type'' column. class CreateProjects < ActiveRecord::Migration def self.up create_table :projects do |t| t.column :type, :string ... end end end How would I convert a ''Project'' object into a ''Goal'' object while preserving other characteristics (id, title, etc.)? Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Just change the value in the type column to change the type to any of the other types. I define a method that aliases to type to make it easier to access. You will need to reload the data to get a different Ruby object. Michael On Mar 27, 1:50 am, "bbebop" <wtbe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have some models which use the same table via single table > inheritance. I (think I) need to change objects from one class to > another and not sure how to go about that. > > Consider these models: > > class Project < ActiveRecord::Base > end > > class Goal < Project # inherits project characteristics > end > > class Assignment < Project # inherits project characteristics > end > > They are distinguished from each other by the ''type'' column. > > class CreateProjects < ActiveRecord::Migration > def self.up > create_table :projects do |t| > t.column :type, :string > ... > end > end > end > > How would I convert a ''Project'' object into a ''Goal'' object while > preserving other characteristics (id, title, etc.)? > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Michael. That''s simple enough! On Mar 27, 8:50 am, "MichaelLatta" <lat...-ee4meeAH724@public.gmane.org> wrote:> Just change the value in the type column to change the type to any of > the other types. I define a method that aliases to type to make it > easier to access. You will need to reload the data to get a different > Ruby object. > > Michael > > On Mar 27, 1:50 am, "bbebop" <wtbe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have some models which use the same table via single table > > inheritance. I (think I) need to change objects from one class to > > another and not sure how to go about that. > > > Consider these models: > > > class Project < ActiveRecord::Base > > end > > > class Goal < Project # inherits project characteristics > > end > > > class Assignment < Project # inherits project characteristics > > end > > > They are distinguished from each other by the ''type'' column. > > > class CreateProjects < ActiveRecord::Migration > > def self.up > > create_table :projects do |t| > > t.column :type, :string > > ... > > end > > end > > end > > > How would I convert a ''Project'' object into a ''Goal'' object while > > preserving other characteristics (id, title, etc.)? > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---