[英]How to get unique record by special column from a where method with Rails?
[英]How to get unique records from Active Record rails 4 based on a column
我有以下型號
顧客
付款方式:customer_id,customer_sign_up,:more_columns
現在,我要為每個customer_id付款。.請記住,我有很多記錄,並且我需要一種良好的性能/最佳方式來做到這一點。
這只會讓我有customer_ids:
Payment.unscoped.where(:customer_sign_up => 1.year.ago..Date.today).uniq.pluk(:customer_id)
這也不起作用:
# #Payment.unscoped.where(params[:type].to_sym =>
# Date.parse(params[:start])..Date.parse(params[:end])
# ).group(["customer_id", "payments.id"]).order("customer_id desc").select("DISTINCT(customer_id), payments.*")
我要整個付款行。
從評論中添加:我只想要一筆付款。 但每個customer_id需付款:
付款:金額,sign_up,customer_id
每個客戶可以有很多付款。 每個客戶肯定會收到很多付款。
而且我不想搜索客戶,而是搜索付款並過濾掉那里的記錄。
customer.payments.where(:customer_sign_up => 1.year.ago..Date.today).order("created_at.desc").first
我最近在這里使用了“訂單”字段,您可以將其調整為想要用作選擇單筆付款標准的任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.