Changing type of ActiveRecord Class in Rails with Single Table Inheritance

Steve’s answer works but since the instance is of class BaseUser when save is called, validations and callbacks defined in User will not run. You’ll probably want to convert the instance using the becomes method: user = BaseUser.where(email: “[email protected]”).first_or_initialize user = user.becomes(User) # convert to instance from BaseUser to User user.type = “User” user.save!

Single Table Inheritance in Django

Summary Django’s proxy models provide the basis for Single Table Inheritance. However, some effort is required to make it work. Skip to the end for a re-usable example. Background Martin Fowler describes Single Table Inheritance (STI) as follows: Single Table Inheritance maps all fields of all classes of an inheritance structure into a single table. … Read more

Best practices to handle routes for STI subclasses in rails

This is the simplest solution I was able to come up with with minimal side effect. class Person < Contact def self.model_name Contact.model_name end end Now url_for @person will map to contact_path as expected. How it works: URL helpers rely on YourModel.model_name to reflect upon the model and generate (amongst many things) singular/plural route keys. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)