[Rails] Re: includes and eager loading not working in simple setup
Great! Thanks everyone for the help.
On Apr 2, 2:11 pm, Colin Law <clan...@googlemail.com> wrote:
> On 2 April 2012 22:03, jenna_s <jenna.sim...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > Ok... I had no idea. I always thought that if you have an
> > includes,that will automatically resolve to a join because otherwise
> > this might result in the N+1 problem. I guess I need to read up about
> > it some more.
>
> > One final question: when I return some_user.messages(another_user.id)
> > to json from my controller, I don't get the from_user and the to_user:
> > format.json { render :json => { :conversation => @messages.as_json } }
> > returns
> > {"conversation":[{"message":
> > {"created_at":"2012-02-25T20:21:01Z","from_user_id":1,"id":
> > 2,"message":"lion king","to_user_id":3,"updated_at":null}},{"message":
> > {"created_at":"2012-02-25T20:21:01Z","from_user_id":3,"id":
> > 9,"message":"this is ninth","to_user_id":1,"updated_at":null}}]}
>
> > How can I force the from_user and to_user as part of each "message"?
> > (I guess I could have them returned as separate tags at the same level
> > as "conversation", but I was wondering if there's a way to do it
> > inside "message", e.g. {"message":
> > {created_at:...","id":"1","from_user":{"name":"Bob","age":"20",
> > etc.}}})
>
> I think you can use the :include option of render :json to do that.
>
> Colin
--
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@googlegroups.com.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home