2011-10-16 51 views
0

的代碼在這裏上市:如何將「rails 2 + mongo_mapper」代碼轉換爲「rails 3 + mongoid」代碼?特別是,函數「set_collection_name」?

它在「梁2 + mongo_mapper」環境編碼的,現在我想將其轉換爲「軌道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環境。內部類。

回答

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 
+0

這是一個有點不同。除了持久化文檔之外,函數「klass」還構建一個名爲「_id.to_s」的類。我想知道你提到的函數「store_in」是否與它相同。 –

+0

你發佈的代碼看起來很奇怪。我不知道爲什麼你使用元編程來定義你的模型,它可能會導致像這樣的奇怪問題。看起來你應該想出一個更好的數據模型,其中你定義的這些類實際上只是可以封裝對象行爲的類的實例。無論如何,'store_in'可以讓你爲該類定義集合名稱,它將用於持久化。要將klass定義爲常量,請使用'Object.const_set(self._id.to_s,klass)'。您可能遇到複數化問題? –

+0

也許我沒有提出我的問題。我在另一個問題中提煉了這個問題:http://stackoverflow.com/questions/7782061/is-it-possible-and-how-to-define-a-class-in-another-classs-function-in-rails- 3 –