当我渲染json时,attr_accessor的属性不显示 [英] Property from attr_accessor does not show when I render json

查看:104
本文介绍了当我渲染json时,attr_accessor的属性不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails中,我有一个User模型.我在模型中添加了attr_accessor :score.

In Rails, I have a User model. I added attr_accessor :scoreto the model.

如果为一个用户呈现JSON,则看不到得分"属性.为什么会这样?

If I render the JSON for one user, I do not see the "score" attribute. Why is this?

user = User.find(3)
user.score = 55
render json: user

推荐答案

render json: user, methods: [:score]

attr_accessor是getter和setter方法的替代方法,因此它是一种方法,正如我已经提到的,我们可以像上面所说的那样

attr_accessor is alternative for getter and setter method so it is a method and as I have mentioned we can call it as above

这篇关于当我渲染json时,attr_accessor的属性不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆