This project is archived and is in readonly mode.

#6710 new
Jake Dempsey

ActiveRecord json serialization should not use keys defined in :methods for included associations

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

I am currently using as_json with something similar to:

def as_json(opts={})

super(:only => [:foo],
  :methods => [:some_method?], 
  :include => { 
    :assoc => { :only => [:bar] }
  }
)

end

When I render my object to json, I notice that some_method? is being rendered in my json hash for the assoc object. The only workaround I have been able to come up with is to pass an empty [] to the :assoc like:

def as_json(opts={})

super(:only => [:foo],
  :methods => [:some_method?], 
  :include => { 
    :assoc => { :only => [:bar], :methods => [] }
  }
)

end

Is that the intended behavior? It does not seem intuitive that the methods declared at the root for the as_json would get inherited by an included association if no methods are defined for the included association. Bug?

Comments and changes to this ticket

  • Jake Dempsey

    Jake Dempsey April 18th, 2011 @ 05:18 AM

    • Title changed from “ActiveRecorder json serialization should not use keys defined in :methods for included associations” to “ActiveRecord json serialization should not use keys defined in :methods for included associations”

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages