Property from attr_accessor does not show when I render json

坚强是说给别人听的谎言 提交于 2020-06-01 04:49:06

问题


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

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

回答1:


render json: user, methods: [:score]

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



来源:https://stackoverflow.com/questions/30641623/property-from-attr-accessor-does-not-show-when-i-render-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!