【问题标题】:How can I make a relationship betwen: User, Medic, Patient, Consultation, Prescription on RoR 4我如何建立以下关系:用户、医生、患者、咨询、ForR 4 上的处方
【发布时间】:2015-10-30 18:45:13
【问题描述】:

我在医疗办公室的一个小项目中建立关系有困难,我正在使用用户登录的设计,对我有意义的关系逻辑是这样的:

  1. 注册用户可以创建个人资料“医生”
  2. 用户可以创建一个新的“患者”(应该与当前的设计 User => 'Medic' 有关)
  3. 新患者有新的“咨询”
  4. 而且咨询有“处方”

所以,我通过以下方式设置模型:

class User < ActiveRecord::Base
  has_one :medic
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

class Medic < ActiveRecord::Base
  belongs_to :user
  has_many :patient
end

class Patient < ActiveRecord::Base
  belongs_to :medic
  has_many :consultum
end

class Consultum < ActiveRecord::Base
  belongs_to :patient
  has_one :prescription
end

class Prescription < ActiveRecord::Base
  belongs_to :consultum
end

对于我运行的脚手架:

rails g scaffold Patient name last_name email phone references:medic

等等……

我觉得我错过了一些东西,¿我怎样才能正确地设置关系而不会在未来出现麻烦?¿还有其他方法(最佳实践)吗?提前致谢。

【问题讨论】:

  • has_many 关系应以复数形式命名,例如has_many :patientshas_many : consulta

标签: ruby-on-rails ruby database-design devise relationship


【解决方案1】:

首先,您不需要与User 关联的单独Medic 模型。

如果您的user 无论如何都将成为medic,为什么不放弃medic 模型而只使用User?如果您更愿意将您的User 模型称为Medic,那就更好了。您必须更改应用中的一些引用:

#config/routes.rb
devise_for :medics

#app/models/medic.rb
class Medic < ActiveRecord::Base
   devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

   has_one :profile
   before_create :build_profile
end

这使您可以访问current_medic

如果您需要为您的医生提供个人资料,那就不同了:

#app/models/profile.rb
class Profile < ActiveRecord::Base
   belongs_to :medic
end

这将允许您致电 @medic.profile - 您宁愿将呼叫信息保留在您的“用户”模型之外。

虽然这似乎离题,但它会极大地帮助您的联想。


#app/models/medic.rb
class Medic < ActiveRecord::Base
   has_many :consultations
   has_many :patients, through: :consultations
end

#app/model/consultation.rb
class Consultation < ActiveRecord::Base
   belongs_to :patient
   belongs_to :medic
   has_one :prescription
end

#app/models/patient.rb
class Patient < ActiveRecord::Base
   has_many :consultations
   has_many :medics, through: :consultations
end

#app/models/prescription.rb
class Prescription < ActiveRecord::Base
   belongs_to :consultation
end

-

检查:

注册用户可以创建个人资料“Medic”

只需使用current_medic

您的患者无需成为用户,只需手动添加即可。只需使用Medic 而不是User,您就可以省去很多麻烦。

我的实现还为医生提供了个人资料:

current_medic.profile

医生可以创建一个新的“病人”

current_medic.patients.new ...

病人有一个新的“咨询”

@consultation = @patient.consultation.new ...
@consultation.medic = Medic.find ...

咨询有'处方'

@consultation.prescription

【讨论】:

    【解决方案2】:

    我认为您的关系做得很好,但如果您真的想阅读一些关于 Rails 关系的优秀文档,请阅读以下内容:

    http://www.toptal.com/ruby-on-rails/top-10-mistakes-that-rails-programmers-make

    其中一个重要的事情是此模型中的字段和类型,如果您选择正确,将更容易保持您的代码高效。

    【讨论】:

      【解决方案3】:

      has_many 需要复数名称。另外我相信:

      class Consultum < ActiveRecord::Base
        belongs_to :patient
        has_many :prescriptions
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-09
        • 2019-06-09
        • 1970-01-01
        • 2014-06-18
        相关资源
        最近更新 更多