I have an object in Rails that has attributes A, B, C, D, and E. When passing this object back to the client-side through a JSON object, how can I tell the rails controller to only include attributes A and D in the JSON object?
Within my Users controller, my code is as follows:
@user = User.find(params[:id]) respond_to do |format| format.html format.json { render :json => @user} end
This code works, however, the JSON object that is returned contains all the attributes of the @user object. How can I limit the attributes that are included in the JSON object before anything is sent back to the client?
UPDATE: lucapette provides some good background about what's happening behind the scenes. Since there are times when I'd probably want all attributes returned, I ended up using the following code:
format.json { render :json => @user.to_json(:only => ["id"])}