Hello, I''m able to create and send a JSON object like so: @mylist << { :id => item.id, :name => name.id } render :json => { :result => ''success'', :mylist => @mylist } That works great. Problem I''m having now is that I need to include users which can be 1 or users per item. @mylist << { :id => item.id, :name => name.id, :users => item.users } Where `item.users` contains a list of (user.id, user.name, user.desc). how do I include an array like users inside a json object? Right now it is including all the user columns which I don''t need. How can I specify which columns to include. Also it appears to be breaking the JSON object, now everything following the first item.users in the JSON object is: ,{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}, {},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}],"result":"success"} Thanks -- 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.
UPDATE the code above is inside a: @items.each_with_index do |item, i| end Perhaps that is a problem here? On Dec 21, 5:07 pm, CuriousNewbie <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, I''m able to create and send a JSON object like so: > > @mylist << { > :id => item.id, > :name => name.id > } > > render :json => { :result => ''success'', :mylist => @mylist } > > That works great. Problem I''m having now is that I need to include > users which can be 1 or users per item. > > @mylist << { > :id => item.id, > :name => name.id, > :users => item.users > } > > Where `item.users` contains a list of (user.id, user.name, user.desc). > > how do I include an array like users inside a json object? Right now > it is including all the user columns which I don''t need. How can I > specify which columns to include. Also it appears to be breaking the > JSON object, now everything following the first item.users in the JSON > object is: > > ,{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}, > {},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}],"result":"success"} > > Thanks-- 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.
On Dec 22, 2010, at 1:28 AM, CuriousNewbie wrote:> UPDATE > > the code above is inside a: > > @items.each_with_index do |item, i| > end > Perhaps that is a problem here? > > > > On Dec 21, 5:07 pm, CuriousNewbie <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hello, I''m able to create and send a JSON object like so: >> >> @mylist << { >> :id => item.id, >> :name => name.id >> } >> >> render :json => { :result => ''success'', :mylist => @mylist } >> >> That works great. Problem I''m having now is that I need to include >> users which can be 1 or users per item. >> >> @mylist << { >> :id => item.id, >> :name => name.id, >> :users => item.users >> } >> >> Where `item.users` contains a list of (user.id, user.name, user.desc). >> >> how do I include an array like users inside a json object? Right now >> it is including all the user columns which I don''t need. How can I >> specify which columns to include. Also it appears to be breaking the >> JSON object, now everything following the first item.users in the JSON >> object is: >> >> ,{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}, >> {},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}],"result":"success"}I think you are running into problems because you are not following strictly the MVC pattern. I assume the code pasted is executed by the controller, correct? Maybe you should look at the as_json method to define json format from the model itself. For example, if Item is a model (inherited from ActiveRecord) and User is too, you should be fine defining two as_json methods inside the two models. That should allow you to render the data in the controller with a simple: render :json => @list If you need more "customization", you should consider redendering json via an erb view (leave the controller alone!!! :D).>> >> Thanks > > -- > 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. >-- 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.
Maybe Matching Threads
- How to Redirect from http://mysite.com to https://www.mysite.com on Herok
- Rails 3 - Nested Forms, using Builder -- Check_box issue
- JSON PUT/POST/UPDATE
- PGError: ERROR: invalid byte sequence for encoding "UTF8": 0xa0
- Rails - escape_javascript without all the \n\n\n\n\n