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 4059366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:11:59+00:00 2026-05-20T15:11:59+00:00

Consider the following: class Parent include Mongoid::Document field:name references_one :child before_create :initialize_child protected def

  • 0

Consider the following:

class Parent
  include Mongoid::Document
  field:name
  references_one :child

  before_create :initialize_child

  protected

  def initialize_child
    self.child = Child.create
  end

end

class Child
  include Mongoid::Document
  field:name
  referenced_in :parent
end

In a console, i get the following weird behavior:

> p = Parent.create
 => #<Parent _id: 4d811748fc15ea355d00000b, name: nil> 
> p.child
 => #<Child _id: 4d811748fc15ea355d00000c, name: nil, parent_id: BSON::ObjectId('4d811748fc15ea355d00000b')> 

All good so far. Now when I try to fetch the parent, and then find the child — no luck …

> p = Parent.last
 => #<Parent _id: 4d811748fc15ea355d00000b, name: nil> 
> p.child
 => nil 

This happens for me with both mongoid rc6 and rc7

Am I doing something wrong (I am new to mongoid) or this a bug? Any work arounds?

Thanks!!

Jonathan

  • 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-05-20T15:12:00+00:00Added an answer on May 20, 2026 at 3:12 pm

    Since the child isn’t embedded, it won’t auto-save it on its own

    Try

    class Parent
      include Mongoid::Document
      field:name
      references_one :child, autosave: true 
    
      before_create :initialize_child
    
      protected
      def initialize_child
        self.child ||= Child.new
      end
    end
    

    Also — you may be expected the Child to be embedded in the Parent document. If so, you’ll want to switch to “embedded_in”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following setup: class Parent < ActiveRecord::Base has_many :children end class Child <
Consider the following class hierarchy: public class Foo { public string Name { get;
Consider the following requirement for building a forum App Parent Post - Child Post1
Consider the following code: var Widget = new Class({ Implements: [Options], options: { name
Consider the following parent/child relationship where Parent is 1..n with Kids (only the relevant
Consider the following code: abstract class X { def a:Unit a } class Y
Consider following code public class City { public string Name { get { return
Consider the following class : class Token: def __init__(self): self.d_dict = {} def __setattr__(self,
Consider the following class definitions class of2010(object): def __init__(self): self._a = 1 self._b =
Consider the following simple polymorphism ... class Parent { public: someFunc() { /* implementation

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.