Using to_json to include nested information from a method

I have a nested method that I would like to output via to_json:

Venuehas a method called published_eventsthat returns published events:

def published_events

    events_array = []

    self.events.each do |event|
      if event.published
        events_array << event
      end
    end

    events_array

  end

Eventhas a method on it to_paramthat I would like to include in json rendering Venue:

format.json { render :json => @venue.to_json(:methods => :published_events => {:include => :to_param}) }

This, of course, does not work to enable the to_param event method. Is there any other way I have to do this, or do I need to build my own Json? Is there a way in the method published_eventsthat I can include in the to_param method?

+5
source share
1 answer
 @venue.to_json(:include => {:published_events => {:method => :to_param}})
+10
source

All Articles