【发布时间】:2020-08-03 08:34:56
【问题描述】:
我的 Rails 6 应用程序中有这些模型:
class Client < ApplicationRecord
belongs_to :account
has_many :people
end
class Person < ApplicationRecord
belongs_to :client
end
class Payment < ApplicationRecord
belongs_to :client
end
在我的SharesController 中,我试图为每个client 生成总付款,并将它们显示为饼图:
class SharesController < ApplicationController
def index
@clients = current_account.clients.joins(:payments)
.where(:payments => {:date => @range, :currency => @currency})
.order("sum_payments_#{@part} DESC")
.group("clients.id", "clients.name")
.having("sum_payments_#{@part} > 0")
.sum("payments.#{@part}")
end
end
问题在于它按client 正确分组。但是,我不想显示每个客户的name,而是想显示每个客户的第一个嵌套person 的last_name。
如何做到这一点?
感谢您的帮助。
【问题讨论】:
标签: sql ruby-on-rails ruby activerecord