Rails: include related object in JSON output

后端 未结 3 1227
面向向阳花
面向向阳花 2021-02-02 09:20

I have a note class that belongs to a user (ie a user can create many notes).

clip from my notes controller

class NotesController < ApplicationControl         


        
3条回答
  •  独厮守ぢ
    2021-02-02 09:51

    Would it be possible to do it the other way around?

    def index
      @user = User.includes(:notes).order("created_at DESC")
      respond_with @user
    end
    

    It would be expensive to include user objects each time the @notes is iterated.

提交回复
热议问题