All, I have a many_to_one relationship object between products and categories and I want to return the category too using JSON results but it seems that rails only gives back the products JSON object on the view layer. This is the piece of code: @products = Product.find(:all, :include => [:category] ) respond_to do |format| format.json {render :json => [@products] end Have I missed something? I thought by doing this I have enabled the eager fetching? Or does eager fetching does not work with JSON results? Thanks in advance. -- If you can''t believe in God the chances are your God is too small. Read my blog: http://joshuajava.wordpress.com/ Follow me on twitter: http://twitter.com/jpartogi --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 14, 1:26 am, Joshua Partogi <joshua.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> All, > > I have a many_to_one relationship object between products and > categories and I want to return the category too using JSON results > but it seems that rails only gives back the products JSON object on > the view layer. This is the piece of code: > > @products = Product.find(:all, :include => [:category] ) > > respond_to do |format| > format.json {render :json => [@products] > end >There''s 2 separate things: the :include option on Product.find just ensures those associations are loaded To have them in your json (or xml) output you need to pass that to to_json or to_xml, ie render :json => @products.to_json(:include => :category) Fred> Have I missed something? I thought by doing this I have enabled the > eager fetching? Or does eager fetching does not work with JSON > results? > > Thanks in advance. > > -- > If you can''t believe in God the chances are your God is too small. > > Read my blog:http://joshuajava.wordpress.com/ > Follow me on twitter:http://twitter.com/jpartogi--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Darn, I returned an array object besides that products instance, giving that to_json will give backslashes to the quotes. On Feb 14, 9:31 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 14, 1:26 am, Joshua Partogi <joshua.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> All, > > > I have a many_to_one relationship object between products and > > categories and I want to return the category too using JSON results > > but it seems that rails only gives back the products JSON object on > > the view layer. This is the piece of code: > > > @products = Product.find(:all, :include => [:category] ) > > > respond_to do |format| > > format.json {render :json => [@products] > > end > > There''s 2 separate things: the :include option on Product.find just > ensures those associations are loaded > To have them in your json (or xml) output you need to pass that to > to_json or to_xml, ie render :json => @products.to_json(:include > => :category) > > Fred > > > Have I missed something? I thought by doing this I have enabled the > > eager fetching? Or does eager fetching does not work with JSON > > results? > > > Thanks in advance. > > > -- > > If you can''t believe in God the chances are your God is too small. > > > Read my blog:http://joshuajava.wordpress.com/ > > Follow me on twitter:http://twitter.com/jpartogi--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---