0

我有这个对象:

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这个模型“序列化”?

4

1 回答 1

0

并非所有 Ruby 类都可以开箱即用地序列化,但幸运的是,哈希是可以序列化的类之一。因此,没有理由为它们编写自己的序列化。但是,如果您这样做并且速度有所提高,那将是最小的。序列化主要用于持久化具有自定义数据类型的字段。

它不会加速您的数据库,但如果您真的想节省空间,您可以为全布尔哈希创建一个自定义类型并将您的哈希序列化为位码。您将创建自定义类并根据您希望如何序列化数据来定义mongoize实例方法、mongoizedemongoize和类方法。evolve是的,这绝对是可能的。然而,在实践中,实现起来很麻烦,并且不会为您节省那么多空间,除非您的哈希值与模型中的其余数据相比很大。

对于您的情况,节省空间的一种更简单的方法是缩短所有字段名称,如下所示:

field :wno, as: :when_new_order, :type => Boolean, :default => "true"
field :wco, as: :when_cancel_order, :type => Boolean, :default => "true" ...

Since the field name is repeated in the database for every document that is stored, long field names will begin to take up a lot of extra space. Since you pass an alias, e.g. :when_new_order, then your model will act exactly like it did before.

于 2013-02-01T04:58:31.347 回答