2

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!

corroded
  • 21,406
  • 19
  • 83
  • 132
Jonathan
  • 16,077
  • 12
  • 67
  • 106
  • 1
    maybe you can override job's to_json in the model so when IR calls it, you'll get the input and output along with it – corroded Jul 21 '11 at 15:33

1 Answers1

5

@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
Jonathan
  • 16,077
  • 12
  • 67
  • 106