我有这个对象:
class MessageSetting
include Mongoid::Document
include Mongoid::Timestamps::Created
#relationships
embedded_in :user
field :when_new_order, :type => Boolean, :default => "true"
field :when_cancel_order, :type => Boolean, :default => "true"
field :when_expire_order, :type => Boolean, :default => "true"
field :when_reject_order, :type => Boolean, :default => "true"
field :when_accept_order, :type => Boolean, :default => "true"
field :when_in_progress_overtime, :type => Boolean, :default => "true"
field :when_is_canceled_due_overtime, :type => Boolean, :default => "true"
field :when_deliver_order, :type => Boolean, :default => "true"
field :when_order_complete, :type => Boolean, :default => "true"
field :when_open_dispute, :type => Boolean, :default => "true"
field :when_buyer_request_refund, :type => Boolean, :default => "true"
field :when_seller_made_refund, :type => Boolean, :default => "true"
field :when_seller_made_dispute_response, :type => Boolean, :default => "true"
field :when_buyer_made_dispute_response, :type => Boolean, :default => "true"
attr_accessible :when_new_order, :when_cancel_order, :when_expire_order, :when_reject_order,
:when_accept_order, :when_in_progress_overtime, :when_is_canceled_due_overtime,
:when_deliver_order, :when_deliver_order, :when_order_complete, :when_open_dispute,
:when_buyer_request_refund, :when_seller_made_refund, :when_seller_made_dispute_response, :when_buyer_made_dispute_response
def active_new_order?
when_new_order?
end
def active_when_cancel_order?
when_cancel_order?
end
def active_when_expire_order?
when_expire_order?
end
def active_when_reject_order?
when_reject_order?
end
def active_when_accept_order?
when_accept_order?
end
def active_when_in_progress_overtime?
when_in_progress_overtime?
end
def active_when_is_canceled_due_overtime?
when_is_canceled_due_overtime?
end
def active_when_deliver_order?
when_deliver_order?
end
def active_when_order_complete?
when_order_complete?
end
def active_when_open_dispute?
when_open_dispute?
end
def active_when_buyer_request_refund?
when_buyer_request_refund?
end
def active_when_seller_made_refund?
when_seller_made_refund?
end
def active_when_seller_made_dispute_response?
when_seller_made_dispute_response?
end
def active_when_buyer_made_dispute_response?
when_buyer_made_dispute_response?
end
end
我知道我可以用 mongoid 进行"序列化":
field :status_message_setting, type: Hash
那么我可以:
m = MessageSetting.new
m.status_message_setting = {:when_new_order => true, :when_cancel_order => false, ...}
m.save
我需要知道参考性能、最佳实践和可用资源,最好使用此模型hash
"序列化"?
并非所有 Ruby 类都可以开箱即用地序列化,但幸运的是,哈希是可以序列化的类之一。因此,没有理由为它们编写自己的序列化。但是,如果您这样做并且速度有所提高,那将是最小的。序列化主要用于持久保存具有自定义数据类型的字段。
它不会加快数据库速度,但如果您真的想节省空间,您可以为全布尔哈希创建自定义类型,并将哈希序列化为位码。您将创建自定义类并定义mongoize
实例方法,并根据要序列化数据的方式mongoize
、demongoize
和evolve
类方法。是的,这绝对是可能的。然而,在实践中,实现起来很麻烦,并且不会为您节省那么多空间,除非您的哈希值与模型中的其他数据相比很大。
对于您的情况,节省空间的更简单方法是像这样缩短所有字段名称:
field :wno, as: :when_new_order, :type => Boolean, :default => "true"
field :wco, as: :when_cancel_order, :type => Boolean, :default => "true" ...
由于数据库中存储的每个文档的字段名称都是重复的,因此长字段名称将开始占用大量额外空间。由于您传递了别名,例如 :when_new_order
,那么您的模型将像以前一样运行。