0

我有以下2个模型:

父模型:

  class Babysitter < ApplicationRecord

  has_many :jobs
  has_many :babysitters, through: :jobs

保姆型号:

  class Babysitter < ApplicationRecord

  has_many :jobs
  has_many :parents, through: :jobs

他们有一个来自 Job 模型的 has_many through 关系:

Model Job:

    class Job < ApplicationRecord
      belongs_to :Babysitter, :touch => true
      belongs_to :Parent
    end

我现在想打电话babysitter.parents,但让每个父母也包括属性薪水(Jobs表中的一个属性)。

这可能吗?

我试过了:

babysitter.parents.includes(:jobs.salary)

此外,是否可以将结果包含在 fastJsonApi 中?

parents_including_salary = babysitter.parents.includes(:jobs.salary)

options = { 包括:[:babysitter, :'parents_including_salary']} json = Api::CampaignReportSerializer.new(otherData, options).serialized_json

4

1 回答 1

0

我认为您可以使用委托来调用薪水?

 belongs_to :Parent
 delegate :salary, to: :parent

你可以试试这个

在您的 API 中可以调用 parent.salary

于 2020-10-22T08:45:56.843 回答