0

I want to retrieve an entire user object via a get request. Currently, on of my methods looks like this:

  def user
    @user = User.find(current_user.id)
    render :json => @user 
  end

This returns the following json:

"{\"annonymous_token\":null,\"email\":\"john1@doe.com\",\"name\":\"john1@doe.com\"}"

My user object / schema has a many-to-many relationship with another model called pictures.

  has_many :picturization
  has_many :pictures, :through => :picturization

How can I modify my code such that returning the user also returns the pictures the user has. If helpful, I am using devise for user authentication.

Thanks!

4

2 回答 2

1

您可以使用.as_json和传递参数,如下所示:https ://stackoverflow.com/a/11336145/308701

所以你可以这样做:

def user
  @user = User.find(current_user.id)
  render :json => @user.as_json(:include => :pictures)
end
于 2012-07-07T17:17:39.850 回答
0

你试过 @user.to_json吗?

于 2012-07-07T17:17:40.523 回答