From a0fe19651edadfbba67b8136cf9e53e2cec9ce39 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 6 Jan 2018 09:31:39 +1300 Subject: [PATCH] Plant parts model using distinct query --- app/models/plant_part.rb | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/app/models/plant_part.rb b/app/models/plant_part.rb index 2590fb966..bbdbae8d2 100644 --- a/app/models/plant_part.rb +++ b/app/models/plant_part.rb @@ -3,21 +3,9 @@ class PlantPart < ApplicationRecord friendly_id :name, use: %i(slugged finders) has_many :harvests - has_many :crops, -> { uniq }, through: :harvests + has_many :crops, -> { distinct }, through: :harvests def to_s name end - - # Postgres complains if the ORDER BY clause of a SELECT DISTINCT query is - # not precisely one of the SELECTed fields. The default sort order on - # crops is lower(name), and Postgres is not smart enough to notice that it - # can calculate this from fields which are selected. The solution is to - # override PlantParts#crops to remove the ORDER BY clause, and replace it - # with `ORDER BY name`. This is not perfect, because it means the crops - # associated to plant parts will not be sorted in the same order as crops - # on the rest of the site. - def crops - super.reorder('name') - end end