如何在Mongoid中为子类设置集合名称?

时间:2015-05-04 18:54:09

标签: ruby mongodb mongoid

class Foo
  include Mongoid::Document
end

class Bar < Foo
end

Foo.all返回Bars,Bar.all返回Foos。

我想把Foo和Bar放在不同的集合中。

我试过

class Bar < Foo
  store_in collection: 'bars'

但得到了

Mongoid::Errors::InvalidStorageParent:
Problem:
  Invalid store_in call on class Bar.
Summary:
  The :store_in macro can only be called on a base Mongoid Document

使用Mongoid 4.0.2

1 个答案:

答案 0 :(得分:10)

You need to make Bar a Mongoid document as well.

class Bar < Foo
  include Mongoid::Document
  store_in collection: 'bars'