postgresql - How to get unique records from Active Record rails 4 based on a column -
i have following models
customer
payments: customer_id, customer_sign_up, :more_columns
now want 1 payment per customer_id.. please remember have many records, , need performance / best way this.
this gets me customer_ids:
payment.unscoped.where(:customer_sign_up => 1.year.ago..date.today).uniq.pluk(:customer_id)
this did not work either:
# #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.*")
i want entire rows of payment.
added comment: dont want 1 payment. 1 payment per customer_id:
payments: amount, sign_up, customer_id
each customer can have many payments. , each customer have many payments sure.
and dont want search on customer rather on payments , filter out records there.
customer.payments.where(:customer_sign_up => 1.year.ago..date.today).order("created_at.desc").first
i've gone recent here order field, can adjust whatever want use criteria choosing single payment.
Comments
Post a Comment