1

I'm trying to make a query that, I believe, is a double joins call.

@customer.jobs.joins(:assignments).where('assignments.employee_id' => current_user.id).collect{|j|j.payments}.flatten

I'm looking for payments, only if they've been assigned to my current_user under my customer record.

Payments are connected to customers through a joins table ( payment_applications ). Employees are assigned to Jobs through a join table ( assignments ).

The query above returns the right objects, but its an array so I can't assign them to pagination. How can I keep that query an ActiveRecord call?

4

1 回答 1

1

如果您想以付款结束,我建议您从那里开始查询:

@payments = Payment.for_customer(customer)

在你的模型中:

class Payment < ActiveRecord::Base
  def self.for_user(user)
    joins(:assignments).where('assignments.employee_id' => user.id)
  end
end

如果您想加入客户记录,请更改为

joins(:assignments => :customers)
于 2013-05-28T14:57:11.153 回答