Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails数据库模式:共享记录_Ruby On Rails_Database_Activerecord_Schema - Fatal编程技术网

Ruby on rails Rails数据库模式:共享记录

Ruby on rails Rails数据库模式:共享记录,ruby-on-rails,database,activerecord,schema,Ruby On Rails,Database,Activerecord,Schema,我有一些有病人的用户。但我想增加用户之间共享患者的功能。目前,我正在SharedPatient表中创建,其中user_id表示与患者共享的用户,patient_id表示患者。这是可行的,但我必须获取User.patients,然后在SharedPatient表中查询他们有权访问的其他患者的ID,然后在Patient表中查询他们的患者记录 我真的只想能够调用User.patients并检索他们共享的患者和他们自己创建的患者。一个布尔值指示用户是否是创建者,这似乎是一种在两者之间进行排序的可靠方法,

我有一些有病人的用户。但我想增加用户之间共享患者的功能。目前,我正在SharedPatient表中创建,其中user_id表示与患者共享的用户,patient_id表示患者。这是可行的,但我必须获取User.patients,然后在SharedPatient表中查询他们有权访问的其他患者的ID,然后在Patient表中查询他们的患者记录

我真的只想能够调用User.patients并检索他们共享的患者和他们自己创建的患者。一个布尔值指示用户是否是创建者,这似乎是一种在两者之间进行排序的可靠方法,但我担心这有点不太妥当。有没有更好的方式来处理这件事,或者我忽略了ActiveRecord的关系

编辑

class用户
我建议在他们之间添加一个关系模型(可以命名为Contact,或者其他名称,我将在下面使用Contact)。然后在该模型上添加一个标志,表明用户是该患者的主要(或创建者或任何您想要的术语)。然后,您可以添加关联以将它们联系在一起:

class User < ActiveRecord::Base
  has_many :contacts, dependent: :destroy
  has_many :patients, through: :contacts
end

class Patient < ActiveRecord::Base
  has_many :contacts, dependent: :destroy
  has_many :users, through: :contacts
end

class Contact < ActiveRecord::Base
  belongs_to :user
  belongs_to :patient
  # has a boolean attribute named primary
  scope :primary, -> { where(primary: true) }
end
class用户{where(primary:true)}
结束

关于问题的第一部分

patient.rb

class Patient < ActiveRecord::Base

:has_and_belongs_to_many :users, :through => :sharedpatients
...
end
class SharedPatient < ActiveRecord::Base
:belongs_to :user
:belongs_to :patient
...

end
以此类推,你明白了

对于第二部分,您应该在patients表中添加一个额外的user\u id字段,例如creator\u id,它将保存创建患者的用户的id。然后在user.rb中:

has_many :created_patients, :class_name =>  "Patient", :foreign_key => 'creator_id'
在您的patient.rb中:

belongs_to :creator, :class_name =>  "User", :foreign_key => 'creator_id'
然后,您将拥有以下方法:

user.created_patients #list of his own patients
patient.creator # who is the doctor who created him

请分享模型的详细信息。你可以查看我关于如何通过thingy创建_的答案的底部…这就是我在第二段中的想法(显然我没有很好地描述它),但我担心我缺少某种更简洁的方法。我在Contact类中添加了一个范围,这可能也很方便。
has_many :created_patients, :class_name =>  "Patient", :foreign_key => 'creator_id'
belongs_to :creator, :class_name =>  "User", :foreign_key => 'creator_id'
user.created_patients #list of his own patients
patient.creator # who is the doctor who created him