I have an Item
model that belongs to a Product
model. Product
has a properties
column of hstore type. I want to return a set of unique items
that belong to a product, and that do not include the current item: @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
Error:
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...