This project is archived and is in readonly mode.

#6711 new
Jake Dempsey

ActiveRecord json serialization should respect as_json definition of included association

Reported by Jake Dempsey | April 18th, 2011 @ 05:23 AM

When I define an as_json override in Question, and provide it with an include for the associated profile, it does not use the as_json defined in the profile model.

class Question < ActiveRecord::Base
def as_json(opts={})

super(:only => [:text],
  :include => [:profile]

end end

class Profile < ActiveRecord::Base
def as_json(opts={})

 super({ :only => [:display_name], :methods => [:avatar] })

end end

This will output the question as json and for the profile association, just output its id field? The only workaround I have found is to not provide an as_json on the profile and duplicate its as_json attrs everywhere I want to include it in a rendering.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket