Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:59:47+00:00 2026-06-04T12:59:47+00:00

I am using Rails 3.2.2 with mongoid 2.4.6. In order to keep my collections

  • 0

I am using Rails 3.2.2 with mongoid 2.4.6. In order to keep my collections small I am storing child objects to a base class in sepparate collections using the “store_in” statement. My code looks like this:

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....

Where does that come from? Is this a bug in mongoid, rails or mongodb?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:59:49+00:00Added an answer on June 4, 2026 at 12:59 pm

    It took me a while but I figured the answer out. I decided to post it, hoping it will help others.

    Mongoid implements something that is called “single table inheritance”. As soon as you derive a child class from a parent class, the child would be stored in the parent collection adding a “type” attribute. Using “store_in” tells mongodb explicitly which collection to store documents in. Defining the store_in in the child class makes mongoid store everything (incl. the parent) in the given collection. I guess using a dedicated store_in assignments for each child messes mongoid up. However, the result is that documents get stored randomly in any of the given collections.

    This can be solved in Ruby using a module as mixin for the common functionality. This is described pretty well in this document.

    BUT I decided not to do this after all! The reason why I wanted this is in order to keep my collections small, hoping to get better performance. After talking to some (10gen) experts I think the better approach is to use the single parent object collection for all child elements. There should be no impact on the performance of mongodb but the solution becomes much more flexible. In fact this makes much better use of the schemaless design in mongodb.

    So the code will look like this again:

    class BaseClass
      include Mongoid::Document
    
      ... shared functionality
    
    end
    
    class ChildClass1 < BaseClass
      ...individual functionality...
    end  
    
    class ChildClass2 < BaseClass
      ...individual functionality...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class User in Rails using Mongoid and Devise. I can't seem
I'm trying to create a questionnaire app in Rails using Mongoid. I keep stumbling
I am using mongoid in rails app. rails 3.0.10 ruby 1.9.2p0 When I am
I would like to write a query in a Rails model using mongoid, and
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
I am currently working on a rails app where we are using mongoid/mongoDB on
I've got a Ruby on Rails app that is using MongoDB for datastorage. Mongoid::Timestamp
I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's to_json method,
I recently played with MongoDB in Rails using Mongoid . I like the ability
I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.