【发布时间】:2019-08-22 06:06:02
【问题描述】:
我遇到了 to_json 无法呈现我的虚拟属性的问题
class Location < ActiveRecord::Base
belongs_to :event
before_create :generate_oid
validates_associated :event
attr_accessor :event_oid
def event_oid
@event_oid = event.oid
end
end
event_oid 不是返回的数组的一部分:
Location.first.attributes当使用自动将记录属性序列化为 jason 的 to_json 时,这对我来说尤其是一个问题。 to_json 省略了我的虚拟属性。
如何将虚拟属性视为实际实例属性?
编辑:
to_json 只是将我的虚拟属性视为实际属性的方法的一个示例。
【问题讨论】:
标签: ruby-on-rails activerecord