LeonS
2011-Mar-04 08:32 UTC
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?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
VelLes
2011-Mar-04 09:32 UTC
Re: Weird problem with json rendering of model of relationship in Rails 3.0.4
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
On Mar 4, 12:32 am, LeonS
<leonard.stellbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Lorenzo Brito Morales
2011-Mar-04 15:33 UTC
Re: Weird problem with json rendering of model of relationship in Rails 3.0.4
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
On Fri, Mar 4, 2011 at 2:32 AM, LeonS
<leonard.stellbrink-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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?
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Luis Galaviz
2011-Mar-04 16:30 UTC
Re: Weird problem with json rendering of model of relationship in Rails 3.0.4
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
On Mar 4, 2:32 am, LeonS
<leonard.stellbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.