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?
I found the answer to this. The ActiveRecord::Base class has a method for this purpose:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002334
The code will be
u = User.find(id)
u = u.becomes(u.type.constantize)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With