How to include two nested associated siblings in json - Rails

时间:2015-11-12 12:14:00

标签: ruby-on-rails json ruby-on-rails-3 api ruby-on-rails-4

I have this structure

user
   -> profile
       -> address (table)
       -> workplace (table)

and i have this code that does not work as expected, it does not output the address nested association

def index
    @users = User.all
    render json: @users.as_json(include: 
                                  [:profile => { 
                                    :include => :address,:include =>:workplace 
                                    }
                                  ]
                                )

  end

1 个答案:

答案 0 :(得分:0)

我设法让它在这里工作是未来参考的答案

def index
    @users = User.all
    render json: @users.as_json(include:
                                  [:profile => {
                                    :include => [:address,:workplace]
                                    }
                                  ]
                                )

  end