Relationship between Customer, Person, Employee, User entity

Hello Friends,

I am trying to define relationship between real-world entities in my
application.... But I am feel, I am over-representing it a little and
hence would like your opinion.

class User < ActiveRecord::Base
  has_and_belongs_to_many :user_groups

  attr_accessible :username, :email, :password, :password_confirmation

end

class Customer < User
  has_many :orders

  belongs_to :person
end

class Employee < User
  belongs_to :person

end

class Person < ActiveRecord::Base
  has_many :addresses
end

Should I merge User and Person Models? Basically user has only login
information while Person has details like first_name, last_name, address
and so on....

Also, should Customer and Employee inherit from Person or User? Trying
to normalize stuff but dont want to over-normalize it too..

Thank you.

Hello Friends,

I am trying to define relationship between real-world entities in my
application.... But I am feel, I am over-representing it a little and
hence would like your opinion.

class User < ActiveRecord::Base
has_and_belongs_to_many :user_groups

attr_accessible :username, :email, :password, :password_confirmation

end

class Customer < User
has_many :orders

belongs_to :person
end

class Employee < User
belongs_to :person

end

class Person < ActiveRecord::Base
has_many :addresses
end

Should I merge User and Person Models? Basically user has only login
information while Person has details like first_name, last_name, address
and so on....

Yes, why complicate matters.

Have you considered that a user might be both a customer and an
employee? Your model would preclude that I think. Are you sure it
would not be better just to have the user model who can be either or
both just by attribute contents?

Colin

I think the differences between Customer and Employee is Role, when you assign a ‘customer’ role, he is a customer.

So you could use Role to distinguish different person, and a person may have many roles

class User < ActiveRecord::Base

has_many :roles

has_many :addresses

end

Colin Law wrote:

Yes, why complicate matters.

Have you considered that a user might be both a customer and an
employee? Your model would preclude that I think. Are you sure it
would not be better just to have the user model who can be either or
both just by attribute contents?

Colin

Valid point :slight_smile:

I do not want to combine Customer and Employee tables because they have
quite different attributes...

Employee has department, manager, roles etc...
Customer has current status, credit card info etc.. bunch of stuff I
will collect on registration.. If I combine them, the resulting table
will be a little too huge..

Assuming you plan to use STI for the tables then the table contains
all the attributes for both anyway. Presumably most of the stuff is
varchar which are variable length fields. so consume very little
space. How many million customers are you expecting so that db size
becomes a factor? :slight_smile:

Colin