Hello, Having trouble with generating some json. I am trying to render an single active record result to json like this: @data = User.find(1) respond_with(@data, :include => :status) The json result is: { -user: { address: null email: "test-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org" first_name: "Test" last_name: "Man" status_id: 1 username: "testguy" status: { } } } So whats the problem? The problem is that the :include=>:status seems to not bring over the relation. In my User model I have a belongs_to :status. How do i get this to work on a single result set? When I do this: @data = User.where("id = 1") respond_with(@data, :include => :status) The relation shows in the json result set fine this way. But its within an array of objects, which i do not want. Any ideas? -- 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.
On May 10, 2011, at 9:02 PM, mikefuzz wrote:> Hello, > > Having trouble with generating some json. I am trying to render an > single active record result to json like this: > > @data = User.find(1)What happens (is there any difference?) if you do this: @data = User.find(1, :include => :status) Walter> respond_with(@data, :include => :status) > > The json result is: > > { > -user: { > address: null > email: "test-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org" > first_name: "Test" > last_name: "Man" > status_id: 1 > username: "testguy" > status: { } > } > } > > So whats the problem? The problem is that the :include=>:status seems > to not bring over the relation. In my User model I have a > belongs_to :status. How do i get this to work on a single result set? > > When I do this: > > @data = User.where("id = 1") > respond_with(@data, :include => :status) > > The relation shows in the json result set fine this way. But its > within an array of objects, which i do not want. > > Any ideas? > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@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.
That doesn''t work. Then it displays this: { -user: { address: null email: "t@test.com" first_name: "Test" last_name: "Man" status_id: 1 username: "test" } } On May 10, 6:02 pm, mikefuzz <m...-iHu0ieuWmYbcIM60gxikxQ@public.gmane.org> wrote:> Hello, > > Having trouble with generating some json. I am trying to render an > single active record result to json like this: > > @data = User.find(1) > respond_with(@data, :include => :status) > > The json result is: > > { > -user: { > address: null > email: "t...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org" > first_name: "Test" > last_name: "Man" > status_id: 1 > username: "testguy" > status: { } > > } > } > > So whats the problem? The problem is that the :include=>:status seems > to not bring over the relation. In my User model I have a > belongs_to :status. How do i get this to work on a single result set? > > When I do this: > > @data = User.where("id = 1") > respond_with(@data, :include => :status) > > The relation shows in the json result set fine this way. But its > within an array of objects, which i do not want. > > Any ideas?-- 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.