2013-05-03 58 views
1

我有一個Item模型屬於Product模型。 Product具有hstore類型的properties列。我想回到一組獨特items屬於一個產品,並且不包括當前項目:@item.similar_itemsPostgresql使用Rails的uniq方法時出錯

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... 

回答

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; 

將您的排序列添加到語句的選擇部分。

相關問題