I believe the easiest way for you to accomplish this will be to create a method in your object to return the avatar URL.
class Model < ActiveRecord::Base
...
def avatar_url
avatar.url(:medium)
end
...
end
This will then allow you to use the methods option when calling to_json with a simple method that does not require any parameters:
respond_to do |format|
render :json => @model.to_json(:only => [:id,:name,:homephone,:cellphone], :methods => [:avatar_url])
end
Which should yield you an output along these lines:
{"id" => 1, "name" => "Cool model", "homephone" => 1234567890, "cellphone" => 0987654321, "avatar_url" => "www.coolsite.com/this_avatars_path"}
See these for reference:
Ruby to_json :methods arguments
http://apidock.com/rails/ActiveRecord/Serialization/to_json
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…