Weird problem with json rendering of model of relationship in Rails 3.0.4

Hi,

I've got a relationship through a one_to_many :though relationship:

Controller:

class TodaysOrdersController < ApplicationController

  respond_to :json

  before_filter :require_user

  authorize_resource :class => false

  def create     @patient = Patient.find(params[:patient_id])     @todays_order_of_patient = DailyOrder.create_or_find_todays_order_for_patient @patient     respond_with(@todays_order_of_patient, :location => nil, :include => :courses)   end end

Model:

class DailyOrder < ActiveRecord::Base

  # Relationships   has_many :course_orders   has_many :courses, :through => :course_orders

  has_many :patient_orders   has_one :patient, :through => :patient_orders

  def self.create_or_find_todays_order_for_patient(patient)     if(!patient.todays_order)       daily_order = DailyOrder.create       PatientOrder.create(:patient => patient, :daily_order => daily_order, :order_for_date => Date.today)       daily_order     else       patient.todays_order     end   end

end

The rendering result of the controller response with something like that: {"marked_for_destruction"=>false, "changed_attributes"=>{}, "attributes"=> {"additional_information"=>"....", "id"=>"594369222"}, "readonly"=>false, "errors"=>{}, "previously_changed"=>{}, "destroyed"=>false, "attributes_cache"=>{}, "new_record"=>false}

But i want the "right"json output which looks like that: {"additional_information"=>"....", "id"=>"594369222"}

Do you know whats wrong here?

Hi , I thing you are trying to get the @todays_order_of_patient object including the child object @todays_order_of_patient.courses in a nested json. If that is what you are trying to accomplish try this:

replace:    respond_with(@todays_order_of_patient, :location => nil, :include => :courses) with this:    respond_with(@todays_order_of_patient.to_json( :include => :courses), :location => nil)

OR version 2 :

remove:    respond_to : json - on the top and replace the controler respond with:

  respond_to do |format|     format.html     format.json { render :json => @todays_order_of_patient object.to_xml(:include => :courses) }   end

In create controller you should put something like respond_to do |format|       format.html # show.html.erb       format.xml { render :xml => @model }       format.json {render :json => @model}     end Also in you have to call it in the browser with .json in order to show it in that format if you just are callin normal it will never render out

Hi LeonS,

When you use respond_with it's render result is always in format HTML unless you specify the contrary thing in your route. For example:

http://localhost:3000/today_orders/15 # This way you will render the HTML

http://localhost:3000/today_orders/15.json # This way you will render the JSON instead the HTML

If you want to set by default to render in JSON format, in your routes you could configure it like this:

resources :today_orders, :defaults => {:action => "create", :format => "json"}

Best, Luis Galaviz