【问题标题】:many-to-many: has_many :through and User table relation issue in rails 4多对多:has_many :through 和 Rails 4 中的用户表关系问题
【发布时间】:2014-12-07 13:15:02
【问题描述】:

我的模型设置:

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

在我的应用程序中登录用户是医生、患者或管理员,我了解到医生和患者的关系如何与预约一起工作,但是如何为此设置用户模型和表格

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

我知道我在这里缺少关键的自我关联,但我该如何做到这一点,或者以任何其他方式为此设置这些模型和表格。提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 associations has-many-through


    【解决方案1】:
    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
    

    如果您正确理解多对多关系,希望这对您有用。

    【讨论】:

    • 医生或病人他们自己是用户。所以他们之间怎么可能有很多归属关系?
    • 用户是唯一的用户,无论他/她是医生还是病人。两者都属于用户。
    • 那么登录用户如何知道他的角色?
    • 那医生和病人怎么办?请告诉我该怎么做?
    • 如果您仍有问题,请访问 Skype:ahmadhasankhan
    【解决方案2】:

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

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

    【讨论】:

    • 我纠正了错字,但我这里的实际问题是关于模型和表格(如果用户),它的样子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2011-04-27
    相关资源
    最近更新 更多