【问题标题】:How to convert a "rails 2 + mongo_mapper" code to "rails 3 + mongoid" code? Especially, the function "set_collection_name"?如何将“rails 2 + mongo_mapper”代码转换为“rails 3 + mongoid”代码?特别是“set_collection_name”函数?
【发布时间】:2011-10-16 01:17:03
【问题描述】:

这里列出了代码

它是在“rails 2 + mongo_mapper”环境中编码的,现在我想将它转换为“rails 3 + mongoid”。没有报告“set_collection_name”的方法名称。

def klass
  @klass ||= user_klass
end

def user_klass
  klass ||= Class.new
  klass.send(:include, Mongoid::Document)
  // is it correct for mongoid
  klass.set_collection_name(self._id.to_s)
  klass.field "created_at", DateTime
  klass.class_eval <<-METHOD
    def id
      self._id.to_s
    end

    def persisted?
      !new_record?
    end
  METHOD

  klass.instance_eval <<-NAME
    def name
      'Row'
    end
  NAME

  self.questions.each do |question|
    klass.field "q#{question.id}", String
    klass.validates_presence_of "q#{question.id}".to_sym, :message => I18n.t('activemodel.errors.messages.blank') if question.required
    klass.validates_uniqueness_of "q#{question.id}".to_sym, :message => I18n.t('activemodel.errors.messages.taken') if question.unique

      if question.input == 'check' || question.input == 'radio'
        klass.class_eval <<-METHOD
        alias_method :old_q#{question.id}=, :q#{question.id}=
        def q#{question.id}=(choices)
          if !choices.is_a?(Array)
            self.old_q#{question.id}= choices
            return
          end

          if choices.include?('_other')
            choices.delete('_other')
            other_options = choices.detect {|c| c.is_a?(Hash)}
            choices << other_options['other']
          end

          choices.reject! {|c| c.is_a?(Hash) || c.blank?}
          self.old_q#{question.id}= choices.join("\n")
        end
      METHOD
    end
  end
  klass
end

函数“send”和“set_collection_name”是否适用于 rails 3 和 mongoid 环境。内部类。

【问题讨论】:

    标签: ruby-on-rails mongoid mongomapper


    【解决方案1】:

    我相信你正在寻找store_in :posts 或类似的东西。

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

      # Macro for setting the collection name to store in.
      #
      # @example Store in a separate collection than the default.
      #   Model.store_in :population
      #
      # @example Store in a capped collection.
      #   Model.store_in :population, :capped => true, :max => 10000
      #
      # @param [ Symbol ] name The name of the collection.
      # @param [ Hash ] options The collection options.
      #
      # @option options [ true, false ] :capped If the collection is capped.
      # @option options [ Integer ] :size The capped collection size.
      # @option options [ Integer ] :max The maximum number of docs in the
      #   capped collection.
      def store_in(name, options = {})
        self.collection_name = name.to_s
        set_collection(options)
      end
    

    【讨论】:

    • 有点不同。除了持久化文档之外,函数“klass”还构建了一个名为“_id.to_s”的类。我想知道你提到的函数“store_in”是否等于它。
    • 您发布的代码看起来很奇怪。我不知道你为什么要使用元编程来定义你的模型,它很可能会导致像这样的奇怪问题。似乎您应该提出一个更好的数据模型,其中您定义的这些类实际上只是可以封装对象行为的类的实例。无论如何,store_in 允许您定义类的集合名称,该名称将用于持久性。要将 klass 定义为常量,请使用 Object.const_set(self._id.to_s, klass)。您可能会遇到复数问题?
    • 也许我没有很好地提出我的问题。我在另一个问题中提炼了这个问题:stackoverflow.com/questions/7782061/…
    猜你喜欢
    • 2023-03-09
    • 2017-12-24
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多