以下是我的查询以获取产品列表:
@listed_products = @products.where(:step => 5, :status => 1)
.includes(:product_attachments)
.includes(:product_reviews)
.order("created_at desc")
结果返回的产品也包含许多类似的country
,例如:
name:prod1
city:city1
country:A
name:prod2
city:city3
country:A
name:prod3
city:city5
country:B
如何从结果查询中过滤出唯一的国家/地区A,B
?我只需要国家列表来构建一个下拉列表,供用户根据国家/地区对产品进行排序。谢谢!
答案 0 :(得分:2)
你可以试试这个 -
@listed_products = @products.where(:step => 5, :status => 1)
.includes(:product_attachments)
.includes(:product_reviews)
.order("products.created_at desc")
result = @listed_products.pluck(:country).uniq
答案 1 :(得分:0)
请尝试以下代码:
@listed_products = @products.where(:step => 5, :status => 1)
.includes(:product_attachments)
.includes(:product_reviews)
.order("created_at desc").uniq.pluck(:country)