eager loading for to_json includes

I need to render some JSON based on my model, and I need to include some associated models. Here's my code to do so.

  def validate_candidate_id 
    matches = User.where(:wca_id => params[:wca_id])
    if matches.any?
      respond_to do |format|
        format.html { redirect_to user_url(matches.first) }
        format.json { render :json => matches.first.to_json(:include => {:candidate_results => {:include => [:checklist_item_results, :assessment]}}) }
      respond_to do |format|
        format.html { render :text => "Invalid ID" }
        format.json { render :json => {}.to_json }

The result. however, is something like this:


As you can see, all of the associated models are missing their attributes. Only their id keys are included. I'm assuming that this has to do with eager loading, that I need to somehow load these records into memory so that their entire contents will be included in the to_json, but how to do that?



I solved the issue by using as_json on my model. I had been using to_json, which was giving me incomplete results. From what I've just read, it seems that as_json is the preferred method for doing this, but I'd never even heard of it!

Need Your Help

Soundcloud waveform nodes

api nodes soundcloud waveform

I was reading a article on soundcloud today about their waveforms and how they generate them by converting the highest volume point into a INT between 0 - 1.

Unrecognized URI: content://mms-sms/

android database sms mms

I want to get all MMS and SMS message records at the same time, but mms-sms seems to be an Unrecognized URI which I copy from android's source...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.