Changing object type?
Is it possible to change an object? I mean, in a STI configuration, let's say I have a class Person and 2 subclasses Employee and Director
Can I update ('promote') an employee to the director staff?
When I try to update an employee, I can change every property of the class except the type
maybe u can create a Employee.promote2Director() method to return a clone of self in Director type... return Director.new(self) ?
I asked this exact question about PHP years ago! http://www.sitepoint.com/forums/showthread.php?t=64171
I think the answer is still "no", you have to go with the spirit of what someone-else is suggesting and synthesize the class upgrade.
A little way's back I wrote an example for doing basically this with single table inheritance:
a_person = Person.new(:name=>'Sam')
a_person.class # this would be 'Person'
a_person.type = 'CoolPerson'
a_person = Person.find(a_person.id)
a_person.class # this would be 'CoolPerson'
Ok, I understand but if it makes sense to promote an employee to a director, what about just fixing a form select error?
I mean it's just a field in the DB (STI -> type column) so if someone is filling a form to create a new object and makes an error for let's say
subclasses Car and Truck, a car can't be updated to a truck in an object approach, doesn't make sense.
Maybe I'm just focusing on stupid things but I don't understand that
I'be read your thread at http://www.sitepoint.com/forums/showthread.php?t=64171
and things are clearer now. I think I've been tangled by the STI thing where everything is in one table. The key is "think 'multiple classes'"
Thanks for the tip :tup: