PG :: UndefinedTable:错误:使用SELECT DISTINCT ON

时间:2015-09-22 18:16:46

标签: ruby-on-rails ruby postgresql activerecord

我正在尝试获取已提交patients的uniq esas_assessment列表,并按esas_assessment created_at的时间排序; clinician has_many patients :through一个名为care_group_assignments的表。{/ p>

我在这里工作的是工作但在我做完时停止工作clinicians has_many :esas_assessments, :through => :patients而不是 esas_assessment clinician_id字段并且直接与临床医生绑在一起。我需要做出这个选择,以便多个临床医生可以与esas_assessment相关联。

这是我在更改esas_assessmentsclinicians的关联方式之前所拥有的。这是基于this answer

<% @esas_assessments.order("patient_id, created_at DESC").select('ON (patient_id) *').uniq.limit(99).each_with_index do |esas_assessment, index| %>

这现在给了我:

  

PG :: AmbiguousColumn:ERROR:ORDER BY&#34; patient_id&#34;含糊不清

我实际上并不理解为什么这些都不明确,因为patientsclinicians都没有字段patient_idcreated_at

为了尝试解决此问题,我使用了this answer并在不明确的字段之前添加了esas_assessment.

<% @esas_assessments.order("esas_assessment.patient_id, esas_assessment.created_at DESC").select('ON (esas_assessment.patient_id) *').uniq.limit(99).each_with_index do |esas_assessment, index| %>

这给了我错误:

  

PG :: UndefinedTable:错误:表格#34; esas_assessment&#34;

缺少FROM子句条目
: SELECT  DISTINCT ON (esas_assessment.patient_id) * FROM "esas_assessments"
INNER JOIN "patients" ON "esas_assessments"."patient_id" = "patients"."id"
INNER JOIN "care_group_assignments"
ON "patients"."id" = "care_group_assignments"."patient_id"
WHERE "care_group_assignments"."clinician_id" = $1
ORDER BY esas_assessment.patient_id, esas_assessment.created_at DESC LIMIT 99

我也尝试使用this answer,因为它似乎是一个类似的问题,但无法弄明白。

任何帮助都会很棒。

Rails 4.1.8,ruby 2.2.1p85,PostgreSQL

1 个答案:

答案 0 :(得分:4)

我相信你在桌面名称上遗漏了s

<% @esas_assessments.order("esas_assessments.patient_id, 
  esas_assessments.created_at DESC")
  .select('ON (esas_assessments.patient_id) *')
  .uniq.limit(99).each_with_index do |esas_assessment, index| %>

表格名称为esas_assessments,而不是esas_assessment