Jonathan
Jonathan

Reputation: 16349

Nested Objects with Inherited Resources

First off, I love inherited_resources

Consider the following:

class Job < ActiveRecord::Base
  has_many :inputs, dependent: :destroy
  has_one :output
end

class JobsController < InheritedResources::Base
  respond_to :json
end

When I request jobs/1.json I just get the JSON of the job object. What I want is also the inputs and output to be included. I normally achieve this by:

job.to_json(include: [:inputs,:output])

My question is what is the best way to achieve this with IR? For now, I'll just overwrite show, but I wanted to know if there was a more elegant way?

Thanks!

Upvotes: 2

Views: 883

Answers (1)

Jonathan
Jonathan

Reputation: 16349

@corroded put me on the right track. The answer is to overwrite as_json on the model.

Specifically I did the following:

  public

  def as_json(options={})
    super(include: [:inputs,:output])
  end

Upvotes: 5

Related Questions