Rails:尝试渲染为JSON时不会显示attr_accessor [英] Rails: attr_accessor doesn't show up when try to render as JSON
问题描述
在我的应用程序中,我有一个下面的模型;
In my application I have a model that is below;
class Init < ActiveRecord::Base
attr_accessor :clientAvailability
attr_accessible :returnCode, :returnMessage, :tokenCode, :updateURL
end
在这里,当我稍后添加** attr_accessor **并尝试应用** render json:init **时,即使设置了它也不会显示** clientAvailability **属性
In here when I've added the **attr_accessor** later on and when I try to apply **render json: init** it doesn't show the **clientAvailability** property even though I've set it as;
init=Init.new
init.clientAvailability="3"
当我调试时,我可以看到它已设置,但是当我尝试将其呈现为json时并不会简单地显示出来.我可能会缺少什么?
When I debug it I can see that it is set, but doesn't simply show up when I try to render it as json. What could be the thing I'm missing?
推荐答案
clientAvailability是数据库中的一列吗?如果没有,那么我认为这是默认方法-仅序列化数据库属性.但您可以这样做:
Is clientAvailability a column in the DB? If not, then I suppose it is the default way - to serialize only the DB attributes. But you can do:
render json: init, methods: [:clientAvailability]
包括clientAvailability属性.
to include clientAvailability attribute.
这篇关于Rails:尝试渲染为JSON时不会显示attr_accessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!