强制ActiveResource不从JSON转换复杂对象



ActiveResource文档声明:

任何复杂元素(包含其他元素的元素)都会成为自己的对象:

# With this response:
# {"id":1,"first":"Tyler","address":{"street":"Paper St.","state":"CA"}}
#
# for GET http://api.people.com:3000/people/1.json
#
tyler = Person.find(1)
tyler.address  # => <Person::Address::xxxxx>

由于我正在检索的对象上的一个属性是一个RGeo对象,它应该是JSON,我如何请求不转换该属性。因此,以上内容将变为:

tyler = Person.find(1)
tyler.address  # => {"street":"Paper St.","state":"CA"}

TRy将其定义为serialize哈希

class Person < ActiveRecord::Base
    serialize :address, Hash
end

然后将address字段视为Hash

@object.address应该返回您,key value

最新更新