【问题标题】:limiting version attributes in mongoid限制 mongoid 中的版本属性
【发布时间】:2012-03-04 13:13:27
【问题描述】:

我正在使用 Mongoid::Versioning,它工作得很好,除了我想防止几个字段被版本化。

文档中没有太多关于它的信息,所以我不知道该怎么做。

http://mongoid.org/docs/extras.html

class Person
  include Mongoid::Document
  include Mongoid::Versioning

  # keep at most 5 versions of a record
  max_versions 5
end

它们展示了如何完全跳过一个版本,但没有展示如何限制某些字段被版本化。

有什么想法吗?

更新

我在挖掘代码时发现了类似的东西,但我不确定如何使用它。

https://github.com/mongoid/mongoid/blob/master/lib/mongoid/versioning.rb#L90

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongodb version mongoid


    【解决方案1】:

    默认情况下所有字段都有选项:versioned true 如果您不希望此版本化,您可以传递 false。例如,我希望名称版本化但没有登录

    class User
      include Mongoid::Document
      include Mongoid::Versioning
    
      field :name, :type => String
      field :login, :type => String, :versioned => false
    end
    

    您也可以在嵌入关联中传递:versioned 选项。

    您可以通过遍历文档上的 .fields 来覆盖此选项。

    因此,在您的代码中,您可以通过创建一个小方法在某些字段上添加避免版本化:

    class User
    
      include Mongoid::Document
      include Mongoid::Versioning
      include Mongoid::Voteable
    
      field :name, :type => String
      field :login, :type => String
    
      def self.avoid_versioned(*unversioned_fields)
        unversioned_fields.each do |f|
    
          fe = self.fields[f.to_s]
          fe.options[:versioned] = false if fe
    
          re = self.relations[f.to_s]
          re[:versioned] = false if re
    
        end
      end
      avoid_versioned( :login, :votes )
    end
    

    【讨论】:

    • 我正在尝试将它与 mongoid_voteable 结合使用,所以我无法将 ':versioned => false' 传递给它。有没有其他方法可以明确告诉它不理会某个嵌入对象?还是选择加入而不是退出?
    • 如果您的所有字段都已定义,我会编写一个方法来完成这项工作。我加你 mongoid_voteable 的例子。
    • 除非我在实现中遗漏了一些东西,否则它似乎不起作用。我得到 Mongoid::Relations::Embedded::Many:Class 的“未定义方法 `foreign_key_suffix” 它仍在尝试对 :votes 关联进行版本化;-(
    • 在 mongoid_voteable 中,投票它不是嵌入你确定使用它吗? github.com/jcoene/mongoid_voteable/blob/master/lib/…
    • 对不起,我用错了名字,它是 voteable_mongo...github.com/angelim/voteable_mongo 它保存了类似 'votes: {"up"=>[], "down"=>[], "faceless_up_count" =>0, "faceless_down_count"=>0, "up_count"=>0, "down_count"=>0, "total_up_count"=>0, "total_down_count"=>0, "count"=>0, "point"= >0, "比率"=>0, "ip"=>[]}'
    【解决方案2】:

    您也许可以找到一种方法来做到这一点,但我建议您改为查看这个 gem。

    https://github.com/aq1018/mongoid-history

    track_history   :on => [:title, :body],       # I want to track title and body fields only. Default is :all
                      :modifier_field => :modifier, # Adds "referened_in :modifier" to track who made the change. Default is :modifier
                      :version_field => :version,   # Adds "field :version, :type => Integer" to track current version. Default is :version
                      :track_create   =>  false,    # Do you want to track document creation? Default is false
                      :track_update   =>  true,     # Do you want to track document updates? Default is true
                      :track_destroy  =>  false,    # Do you want to track document destruction? Default is false
    

    【讨论】:

    • 我喜欢它,但我想看看是否还有另一种不使用其他 gem 的可能性?
    【解决方案3】:

    您可以随时通过将持久性调用包装在无版本块中来跳过版本控制。

    class Person
      include Mongoid::Document
      include Mongoid::Versioning
    end
    
    person.versionless do |doc|
      doc.update_attributes(name: "Theodore")
    end
    

    【讨论】:

    • 我在文档中看到了这一点,但我不需要按记录进行。我需要它永久用于特定领域。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    相关资源
    最近更新 更多