如何在Ransack中使用子选择?

时间:2019-06-04 07:54:27

标签: ruby-on-rails-5 ransack kaminari

(我不确定我选择的标题是否合适。欢迎提出建议。)

我无法在索引视图中列出所有患者记录以及相关表中的其他信息。特别是,如果没有重复患​​者,我将无法获得随访领域。 这是我要获取的示例(前两个字段来自Patient表,而dt_date字段来自后续表):

|-----------------------------------|
|first_name last_name   dt_date     |
|-----------------------------------|
|john       Doe         2011-11-01  |
|Mary       Green       1999-06-03  |
|Mark       Red         2009-05-07  |
|...                                |
|-----------------------------------|

Patient.rb:(工作,没有dt_field)

    class Patient < ApplicationRecord
        has_many :followups, dependent: :destroy

       scope :basic_info, -> { 
        self.left_joins(hospitalizations: :surgery)
           .distinct
           .select("patients.id,first_name, 
                    last_name, 
                    MAX(surgeries.surgery_date) as most_recent_surgery")
           .group(:id, :first_name, :last_name)
       }

    end

followup.rb:

    class Followup < ApplicationRecord
        belongs_to :patient, inverse_of: :followups
    end

followups_controller.rb:

    @p = Patient.basic_info.ransack(params[:q])
    @patients = @p.result.page(params[:page])

现在,我需要在患者的索引视图中显示随访的字段“ dt_date”,但是如果我加入随访表,则会得到重复(我不想要):

    scope :basic_info, -> { 
        self.left_joins(hospitalizations: :surgery)
           .distinct
           .select("patients.id,first_name, 
                    last_name, 
                    MAX(surgeries.surgery_date) as most_recent_surgery")
          .left_joins(:followups)
          .select("dt_date")
          .group(:id, :first_name, :last_name, :dt_date)
    }

结果:

|-----------------------------------|
|first_name last_name   dt_date     |
|-----------------------------------|
|john       Doe         2011-11-01  |
|john       Doe                     |
|Mary       Green       1999-06-03  |
|Mark       Red         2009-05-07  |
|...                                |
|-----------------------------------|

因此,我尝试以这种方式修改代码:

    scope :basic_info, -> { 
        self.left_joins(hospitalizations: :surgery)
           .distinct
           .select("patients.id,first_name, 
                    last_name, 
                    MAX(surgeries.surgery_date) as most_recent_surgery,
               (select f.dt_date from followups f where f.dt_date is not null
                and  f.patient_id = patients.id ) as dt_date")
           .group(:id, :first_name, :last_name, :dt_date)
    }

但是,当Kaminari分页到位时,我收到一个MySQL错误(如果只显示一页,则没有错误):

Mysql2::Error: Unknown column 'dt_date' in 'field list': SELECT DISTINCT
COUNT(DISTINCT `patients`.`id`) AS count_id, `patients`.`id` AS patients_id,
`patients`.`first_name` AS patients_first_name, `patients`.`last_name` AS
patients_last_name, `dt_date` AS dt_date FROM `patients` LEFT OUTER JOIN
`hospitalizations` ON `hospitalizations`.`patient_id` = `patients`.`id`
LEFT OUTER JOIN `surgeries` ON `surgeries`.`hospitalization_id` = 
`hospitalizations`.`id` GROUP BY `patients`.`id`,
`patients`.`first_name`, `patients`.`last_name`, `dt_date`

Ransack似乎删除了子选择(我想是因为它缺少Activerecord:Relation

哪个是解决此问题的正确代码?

我正在使用Rails 5.0.2,Ransack和Kaminari。

0 个答案:

没有答案