1

我对车型设置:许多一对多:的has_many:通过和用户表关系问题导轨4

class Doctor < ActiveRecord::Base 
    has_many :appointments 
    has_many :patients, :through => :appointments 
end 

class Appointment < ActiveRecord::Base 
    belongs_to :doctor 
    belongs_to :patient 
end 

class Patient < ActiveRecord::Base 
    has_many :appointments 
    has_many :physicians, :through => :appointments 
end 

在我的应用程序LogedIn-用户是无论是医生,病人或我得到了管理员明白如何医生,并与预约医患关系的工作,但如何建立用户模型,并表为

class User_type < ActiveRecord::Base 
    belongs_to :doctors, class_name: "USER" 
    belongs_to :patients, class_name: "USER" 
end 

我知道我在这里失去了关键的自我协会,但我怎么能做到这一点,或任何其他方式为此设置这些模型和表格。提前致谢。

回答

0

你有错字在这里:class User_type < ActiveRecord::Base belongs-to :doctors, class_name: "USER" belongs_to :patients, class_name: "USER" end

应该belongs_to,你肯定那不是你的问题?

+0

我纠正了错字,但我在这里的实际问题是关于模型和表,如果用户,它是什么样子 – 2014-12-07 13:21:53

2
class User < ActiveRecord::Base 
    has_one :doctor 
    has_one :patient 
end 

class Doctor < ActiveRecord::Base 
    belongs_to :user 
    has_many :appointments 
    has_many :patients, :through => :appointments 
end 

class Appointment < ActiveRecord::Base 
    belongs_to :physician 
    belongs_to :patient 
end 

class Patient < ActiveRecord::Base 
    belongs_to :user 
    has_many :appointments 
    has_many :physicians, :through => :appointments 
end 

希望这会对你有用,如果你能正确理解多对多的关系。

+0

医生或病人他们他们自己是user.so他们之间如何可以有许多belongs_to关系是可能? – 2014-12-07 13:32:52

+0

无论用户是医生还是患者,用户都是用户。 都属于用户。 – 2014-12-07 13:35:11

+0

那么登录用户会如何知道他的角色? – 2014-12-07 13:42:43