English 中文(简体)
Mongodos 仓库产生随机结果
原标题:Mongoid store_in produces random results

我使用铁路3.2.2和蒙戈伊德 2.4.6。 为了保持我的收藏规模小,我使用“store_ in” 语句将儿童物品储存在Sepparate收藏中的底座。 我的代码是这样的:

class BaseClass
  include Mongoid::Document
end

class ChildClass1 < BaseClass
  store_in :child_1
end  

class ChildClass2 < BaseClass
  store_in :child_2
end

It appears that the objects get randomly stored in or or the other child collection. An object of type Child1 sometimes gets stored in collection Child2. Here is the surprising thing that I see in my logs:

Started POST "/child_class_1" for 127.0.0.1 at 2012-05-22 10:22:51 -0400
Processing by ChildClass1Controller#create as HTML

MONGODB (0ms) myproject_development[ child_2 ].insert....

那是从哪来的?这是蒙戈伊德、铁轨或蒙戈巴的虫子吗?

最佳回答

花了我一阵子,但我找到了答案。我决定张贴它,希望它能帮助别人。

Mongoid 执行一个叫做“ 单桌继承” 的东西。 一旦您从父类获得一个儿童类, 儿童将被存储在父类收藏中, 添加一个“ 类型” 属性 。 使用“ store_ in” 明确地告诉 mongodb 哪个收藏可以存储文件 。 定义在儿童类中的存储点使 Mongoid 在给定的收藏中存储了所有内容( 包括父类) 。 我想对每个孩子都使用专门的存储点。 但是, 结果是文件随机存储在给定的收藏中 。

Ruby可以用一个模块作为通用功能的混音来解决这个问题。这份文件对此描述得相当清楚。

我决定不要这样做! 我之所以想要这样做, 是因为为了保持我的收藏数量小, 希望取得更好的业绩。 在与一些专家( 10 gen) 交谈之后, 我认为更好的办法是使用所有儿童元素的单亲对象收藏。 不应该对蒙戈德的性能产生影响, 但解决方案要灵活得多。 事实上, 这可以更好地利用蒙戈德布的无计划设计。

所以代码会再次看起来这样:

class BaseClass
  include Mongoid::Document

  ... shared functionality

end

class ChildClass1 < BaseClass
  ...individual functionality...
end  

class ChildClass2 < BaseClass
  ...individual functionality...
end
问题回答

暂无回答




相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签