Typecasting base class models in Single Table Inheritence(STI) scenario
ruby, ruby-on-rails
Solution
Due to a bug in STI, the ActiveRecord finder was returning a base class instance(User) instead of subclass instance(Consumer). I thought this is the default behavior and hence wanted to cast the base class instance(User) to the sub class instance(Consumer). My previous solution became redundant after addressing the bug. i.e.
u = User.find(id) # returns an instance of Consumer class
Problem
``` class User < ActiveRecord::Base end class Consumer < User end class Merchant < User end u = User.find(id) ``` How do I type cast the variable `u` to the type Consumer?