gpt4 book ai didi

ruby-on-rails - 使用 Rails 的 uniq 方法时出现 Postgresql 错误

转载 作者:行者123 更新时间:2023-11-29 13:34:32 25 4
gpt4 key购买 nike

我有一个属于 Product 模型的 Item 模型。 Product 有一个 hstore 类型的 properties 列。我想返回属于某个产品的一组唯一项目,但不包括当前项目:@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...

最佳答案

如错误消息所述...

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;

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

关于ruby-on-rails - 使用 Rails 的 uniq 方法时出现 Postgresql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16361550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com