Item
モデルに属するモデルがありProduct
ます。hstore タイプProduct
の列があります。製品に属し、現在のアイテムを含まない一意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...