簡體   English   中英

Ruby on Rails 從查詢結果中獲取唯一的國家/地區

[英]Ruby on Rails get unique country from Query results

以下是我獲取產品列表的查詢:

@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 我只需要國家/地區列表來構建一個下拉列表,供用戶根據國家/地區對產品進行排序。 謝謝!!

你可以試試這個——

@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

試試下面的代碼:

   @listed_products = @products.where(:step => 5, :status => 1)
                            .includes(:product_attachments)
                            .includes(:product_reviews)
                            .order("created_at desc").uniq.pluck(:country)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM