I am using Ruby on Rails 3 and I implemented a working Single Table Inheritance like the following:
class User < ActiveRecord::Base
# Schema Information
#
# Table name: User
#
# id : integer
# type : string
# children_user_id: integer
...
end
class UserAdmin < User
# Schema Information
#
# Table name: UserAdmin
#
# id : integer
# special_field1 : string
# special_field2 : string
# ...
...
end
class UserCommon < User
# Schema Information
#
# Table name: UserCommon
#
# id : integer
# another_field1 : string
# another_field2 : string
# ...
...
end
I would like to know if, on creating an UserAdmin
record (or an UserCommon
record) in the User
table running the following
UserAdmin.create(:children_user_id => "1")
# or UserCommon.create(:children_user_id => "1")
it is possible to "automatically" create in someway (possibly the "Rails Way"!) a new record also in the UserAdmin
table (or UserCommon
table) that has its own fields (at the database level, those fields are columns). I would like to do that in order to "better handle" UserAdmin
because this class has different and more attributes of the UserCommon
class.
If it is possible, how can I do that (maybe using association model statements, callbacks, polymorphism, ...)? Do you have some advice on this issue?