我只是发现rails_timestamps正在膨胀我的所有请求,事实上,在许多情况下,没有。时间戳消耗的字符比我们实际需要的原始内容还要多。
在使用断点进行了大量调试之后,我终于重写了serializable_hash,而不是重写as_json,当使用:include =>时,似乎不递归地调用它。
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
# Ignore created_at and updated_at by default in JSONs
# and when needed add them to :include
def serializable_hash(options={})
options[:except] ||= []
options[:except] << :created_at unless (options[:include] == :created_at) || (options[:include].kind_of?(Array) && (options[:include].include? :created_at))
options[:except] << :updated_at unless (options[:include] == :updated_at) || (options[:include].kind_of?(Array) && (options[:include].include? :updated_at))
options.delete(:include) if options[:include] == :created_at
options.delete(:include) if options[:include] == :updated_at
options[:include] -= [:created_at, :updated_at] if options[:include].kind_of?(Array)
super(options)
end
end到目前为止,它工作得很好,满足了我从基本到中等的所有需求,但后来我明白我需要继续前进,开始使用active-model-serializers,但从文档来看,似乎'serializable_hash`在许多地方都被使用了(幸运的是,它没有中断……)。,我这样做对吗?
这会破坏我的密码吗?我要面对什么后果?
我对此有疑问:
Orders这样的模型,以便在下订单或更新订单时得到它们。:include ,就像我们对associated_models那样?类似于序列化器/application_erializer.rb类ApplicationSerializer models/application_record.rb中的serializable_hash中有类似的东西。发布于 2016-11-30 07:20:56
尝试添加序列化器/application_erializer.rb
def filter(keys)
keys.delete :created_at, :updated_at
super(keys)
endhttps://stackoverflow.com/questions/40839687
复制相似问题