[英]How to return array instead of array of object in rails json response
[英]Rails how to return single JSON object instead of array of JSON objects?
我限制为1,因此我认为在这种情况下,它将简单地返回一个与.find_by_email
相同的对象
码:
# GET /users/:identified/type/:social_type
# Returns a single record - so limit 1
def find
@user = User.where("identified = ? AND social_type = ?", params[:identified], params[:social_type]).limit(1)
if not @user.empty?
render json: @user.as_json, status: :created
else
render json: @user, status: :not_found
end
end
当前响应:
[{"id":7,"voy_num":null,"voy_pin":null}]
如何确保我返回单个JSON object
?
要获取单个对象,请first
与以下where
一起使用:
@user = User.where("identified = ? AND social_type = ?", params[:identified], params[:social_type]).first
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.