Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the collection name in Mongoid for a subclass?

class Foo
  include Mongoid::Document
end

class Bar < Foo
end

Foo.all returns Bars, and Bar.all returns Foos.

I want to put Foo and Bar in separate collections.

I tried

class Bar < Foo
  store_in collection: 'bars'

but got

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

Using Mongoid 4.0.2

like image 808
B Seven Avatar asked May 04 '15 18:05

B Seven


1 Answers

You need to make Bar a Mongoid document as well.

class Bar < Foo
  include Mongoid::Document
  store_in collection: 'bars'
like image 154
Rob Wagner Avatar answered Nov 11 '22 22:11

Rob Wagner