Should a one-to-one relationship, column, or something else be used?

Although my problem is specifically in Ruby on Rails, I also ask in general: I have a user table and you want some users to become students and others to be teachers. What is the best way to design a circuit in this case?

Edit: Although I initially accepted vonconrad's answer - it meets the original criteria - I need to open and configure the question again based on feedback from nhnb.

What if students and / or teachers need additional (unique) attributes?

+3
source share
2 answers

, , . , , :

  • , , , . , , (has_many :user, :polymorphic => true)
  • . : users user_attributes. users id, user_type, username, etc. user_attributes id, user_id, attribute_name, value.

, , , .

!

+2

teacher. true, . , . .

(, , , , janitor (?)), UserGroup. user_group_id. . :

class User < ActiveRecord::Base
  belongs_to :user_group
end

class UserGroup < ActiveRecord::Base
  has_many :users
end

.

+2

Source: https://habr.com/ru/post/1782062/


All Articles