开发者

Unable to see attr_accessors when transforming object to JSON

开发者 https://www.devze.com 2023-03-26 02:58 出处:网络
I have a class with attr_accessors. When I want to create a JSON string of my object, my attr_accessors are not listed, only attributes created with mongo.

I have a class with attr_accessors. When I want to create a JSON string of my object, my attr_accessors are not listed, only attributes created with mongo.

class Weblink < Media
  field :title, :type => 开发者_如何学JAVAString
  field :url, :type   => String
  attachment :image

  attr_accessor :images, :domain_name, :image_width, :image_height
end

The generated JSON

{"_id":"4e3ab9895d156dae63000001","_type":"Weblink","description":"Some description","title":"Some title","url":"http://some_url.com"}

I would like to have for exemple, my "images" attributes included.

Any suggestions ?


try:

weblink.to_json(:methods=>[:images, :domain_name, :image_width, :image_height])
0

精彩评论

暂无评论...
验证码 换一张
取 消