不知道为什么MongoMapper不能处理一个拥有和属于许多关系



这充满了问题,与我在SQL上看到的任何东西都不同。我只是试图创建一个HABTM关系并使对象相互匹配。

我的两个模型

class Word
  include MongoMapper::Document         
  many :topics
  key :word, String
end
class Topic
  include MongoMapper::Document         
  many :words
  key :name, String
end

该模型单独工作允许我创建对象并关联它们。我喜欢蒙戈的部分原因。

然后我尝试像这样取一些样本 yaml:

Music I Dig:
  reggae:
    bob marley
    isaac hayes
    groundation
  classical:
    philip glass
    bach

并尝试使用此 Rakefile 解析它:

File.open(Rails.root + 'lib/words/disgusting_glass_full_of.yml', 'r') do |file|
  YAML::load(file).each do |topic, word_types|
    puts "Adding #{topic}.."
    @temp_topic = Topic.create name: topic
    @temp_words = word_types.map { |type, words|
      words.split(' ').map{ |word| 
        @word = Word.create type: type, word: word
        @word.topics << @temp_topic
      }
    }
    @temp_topic.words << @temp_words.flatten
  end
end

我不骗你,这是我见过的最随机、最混乱的输出。 2 创建的实际主题数量为空且没有数据。有些主题有关联,有些主题已完成。与文字相同。有些单词会随机产生关联,而其他单词则不会。我根本找不到任何关于它如何得出这个结果的联系。

我相信问题出在我如何设置模型(也许?如果没有,我就扔mongo_mapper并尝试Mongoid。

首先,您需要指定模型中的数组属性word_ids和topic_ids:

  class Topic
    include MongoMapper::Document         
    many :words, :in => :word_ids
    key :word_ids, Array
    key :name, String
  end
class Word
  include MongoMapper::Document         
  many :topics, :in => :topic_ids
  key :topic_ids, Array
  key :word, String
end

您还必须确保在 rake 任务中保存您的主题和单词:

task :import => :environment do
  File.open(Rails.root + 'lib/test.yml', 'r') do |file|
    YAML::load(file).each do |topic, word_types|
      puts "Adding #{topic}.."
      temp_topic = Topic.create name: topic
      temp_words = []
      word_types.map do |type, words|
        words.split(' ').map do |word|
          word = Word.create type: type, word: word
          word.topics << temp_topic
          word.save
          temp_words << word
        end
      end
      temp_topic.words << temp_words.flatten
      temp_topic.save
    end
  end  
end

这给了我以下输出:

{
    "_id" : ObjectId("502bc54a3005c83a3a000006"),
    "topic_ids" : [
        ObjectId("502bc54a3005c83a3a000001")
    ],
    "word" : "groundation",
    "type" : "reggae"
}
{
    "_id" : ObjectId("502bc54a3005c83a3a000007"),
    "topic_ids" : [
        ObjectId("502bc54a3005c83a3a000001")
    ],
    "word" : "philip",
    "type" : "classical"
}  ....etc

{
    "_id" : ObjectId("502bc54a3005c83a3a000001"),
    "word_ids" : [
        ObjectId("502bc54a3005c83a3a000002"),
        ObjectId("502bc54a3005c83a3a000003"),
        ObjectId("502bc54a3005c83a3a000004"),
        ObjectId("502bc54a3005c83a3a000005"),
        ObjectId("502bc54a3005c83a3a000006"),
        ObjectId("502bc54a3005c83a3a000007"),
        ObjectId("502bc54a3005c83a3a000008"),
        ObjectId("502bc54a3005c83a3a000009")
    ],
    "name" : "Music I Dig"
}

相关内容

最新更新