我有一个属于Item
模型的Product
模型。 Product
的hstore类型为properties
列。我想返回一组属于产品的唯一 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...
答案 0 :(得分: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;
将排序列添加到语句的选择部分。