0

I have the following 2 models:

Parent Model:

  class Babysitter < ApplicationRecord

  has_many :jobs
  has_many :babysitters, through: :jobs

Babysitter Model:

  class Babysitter < ApplicationRecord

  has_many :jobs
  has_many :parents, through: :jobs

They have a has_many through relationship from the Job model:

Model Job:

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

I now want to call babysitter.parents but have each parent also include the attribute salary (an attribute from the Jobs table).

Is this possible?

I tried:

babysitter.parents.includes(:jobs.salary)

Additionally is it possible to include the result in a fastJsonApi?

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

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

lost9123193
  • 10,460
  • 26
  • 73
  • 113
  • check out https://scoutapm.com/blog/activerecord-includes-vs-joins-vs-preload-vs-eager_load-when-and-where – Mark Oct 21 '20 at 19:03

1 Answers1

0

I think you can use delegate to call the salary?

 belongs_to :Parent
 delegate :salary, to: :parent

you can try this

in your API can call parent.salary

wiwit
  • 73
  • 1
  • 7
  • 26