Sql HABTM联接表上的Rails筛选条件 class医生患者#1等
如何在一次查询中访问具有体检的医生患者列表?反之亦然(患者有不同类型的不同预约)?然后我可以用类似于Sql HABTM联接表上的Rails筛选条件 class医生患者#1等,sql,ruby-on-rails,activerecord,scope,has-and-belongs-to-many,Sql,Ruby On Rails,Activerecord,Scope,Has And Belongs To Many,如何在一次查询中访问具有体检的医生患者列表?反之亦然(患者有不同类型的不同预约)?然后我可以用类似于medical.patients\u with\u physicals=[patient]?的东西来设置它吗以下内容将允许您在单个查询中获取任何类型的患者: class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments end c
medical.patients\u with\u physicals=[patient]
?的东西来设置它吗以下内容将允许您在单个查询中获取任何类型的患者:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
scope :physicals, -> { where appointment_type: 'physical' }
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
class医生{:type=>type})
结束
结束
您希望在哪里使用medical.patients\u和\u physicals=[患者]
您打算如何填写我认为需要的其他预约数据(时间等)
值得注意的是,type
作为一个列被Rails用来表示一个使用单表继承(STI)的模型,它可能会给您带来问题,因此我建议使用不同的列名
我如何才能访问一份患者列表,对于一名医生,在一个
单一查询
使用:
#app/models/medicine.rb
类@doctor.patients.with(“体检”)
#->患者#1等
你能解释一下你的意思吗
有不同类型不同预约的患者)
首先,它不是
HABTM
。它是has\u many=>to
。我会将该字段更改为其他类型。您是否回答了如何设置剩余约会数据(时间、日期等)的问题?当通过方法(如问题中所述)添加患者时,有多种方法可以设置预约类型,但如果要考虑其他数据,可能有更好的方法。
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
def patients_with_appointment_of_type type
self.patients.joins(:appointments)
.where(:appointments => {:type => type})
end
end
#app/models/physician.rb
Class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments do
def with(type)
where(appointment_type: type)
end
end
end
#-> @doctor.patients.with("physicals")
#-> Patient #1 etc