Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#6710

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?

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

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Jake Dempsey
    Jake Dempsey
    • 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

    April 18th, 2011 @ 05:18 AM