ruby on rails - Inheritance from ActiveRecord Object -


lets there activerecord class called user, representative of user table of database.

but have different type of users have

  • special functions
  • special variables
  • custom relations (employer has_many companies, employee belongs_to company :)

but these users have lot of functionality in common. want create classes each different type of user inherit them user class.

  • user < activerecord::base
  • employer < user
  • employee < user
  • customer < user

what best way of doing that?

thanks

a lot of applications start out user model of sort. on time, different kinds of users emerge, might make sense make greater distinction between them. admin , guest classes introduced, subclasses of user. now, shared behavior can reside in user, , subtype behavior can pushed down subclasses. however, user data can still reside in users table.

all need add type column users table hold name of class instantiated given row. active record takes care of instantiating kind of object when loads database.

this technique called single table inheritance or sti (for short).

a recent article sti here: http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -