1

我有一个Item属于模型的Product模型。Product有一properties列 hstore 类型。我想返回一组属于产品的唯一且不包括当前项目的: items@item.similar_items

class Item < ActiveRecord::Base
  belongs_to :product
  scope :by_platform, ->(value) { joins(:product).merge(Product.by_platform(value)) }
  scope :by_genre, ->(value) { joins(:product).merge(Product.by_genre(value)) }

  def similar_items
    Item.includes(:product)
      .by_platform(self.product_platform)
      .by_genre(self.product_genre)
      .limit(3).where.not(product: self.product)
      .order("(properties -> 'release_date')::date DESC")
      .uniq
  end
end

class Product < ActiveRecord::Base
  store_accessor :properties, :platform, :genre
  has_many :items

  scope :by_platform, ->(value) { where("properties @> hstore('platform', ?)", value) }
  scope :by_genre, ->(value) { where("properties @> hstore('genre', ?)", value) }
end

错误:

PG::Error: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...e')) AND ("items"."product_id" != 426)  ORDER BY (properties...
4

1 回答 1

1

正如错误消息所述...

test=> select distinct id from test order by f;
ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: select distinct id from test order by f;

将排序列添加到语句的选择部分。

于 2013-05-03T16:16:35.577 回答