【发布时间】:2019-01-01 16:21:41
【问题描述】:
我有三个 Rails 模型和关联:
class Account < ApplicationRecord
has_many :bills
belongs_to :user
end
class Bill < ApplicationRecord
belongs_to :account
end
class User < ApplicationRecord
has_many :accounts
end
我正在尝试创建一个端点来返回所有用户及其帐户以及帐户的所有账单。
现在我可以通过以下方式获取所有账单及其帐户:
bill_records = Bill.all.includes(:account)
bill_records_with_associations = bill_records.map do |record|
record.attributes.merge(
'account' => record.account,
)
但现在我需要获取与每个帐户关联的用户,我不知所措。
我也可以在这里检索用户吗?
【问题讨论】:
标签: ruby-on-rails activerecord