如何根据列从Active Record rails 4获取唯一记录

时间:2014-11-19 14:49:21

标签: ruby-on-rails postgresql ruby-on-rails-4 rails-activerecord

我有以下模特

客户

付款: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

每位客户都可以获得多笔付款。 每个客户肯定会有很多付款。

我不想搜索客户,而是搜索付款并过滤掉那里的记录。

1 个答案:

答案 0 :(得分:0)

customer.payments.where(:customer_sign_up => 1.year.ago..Date.today).order("created_at.desc").first

我已经在订单字段中找到了最新版本,您可以将其调整为您想要用作选择单笔付款的条件。