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