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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:08:17+00:00 2026-05-26T20:08:17+00:00

I have a problem with relationships persisting in an embedded document. Here is the

  • 0

I have a problem with relationships persisting in an embedded document. Here is the example

require 'mongoid'
require 'mongo'

class User
  include Mongoid::Document
  field :name
  key :name

  embeds_one :garage
end

class Garage
  include Mongoid::Document
  field :name

  has_many :tools, autosave: true
  has_many :cars, autosave: true
end

class Tool
  include Mongoid::Document
  belongs_to :garage, inverse_of: :tools
end

class Car
  include Mongoid::Document
  field :name

  belongs_to :garage, inverse_of: :cars
end

When I run the following:

Mongoid.configure do |config|
  config.master = Mongo::Connection.new.db("mydb")
end

connection = Mongo::Connection.new
connection.drop_database("mydb")
database = connection.db("mydb")


user = User.create!(name: "John")
user.build_garage
user.garage.cars << Car.create!(name: "Bessy")

user.save!
puts "user #{user}, #{user.name}"

user.garage.cars.each do |car|
  puts "car is #{car}"
end

same_user = User.first(conditions: {name: user.name})

puts "same_user #{same_user}, #{same_user.name}"
same_user.garage.cars.each do |car|
  puts "car found is #{car}"
end

the output is:

user #<User:0x00000003619d30>, John
car is #<Car:0x00000003573ca0>
same_user #<User:0x000000034ff760>, John

so the second time the user’s car array is not outputted. How do i get the array of cars to persist?

Thanks

  • 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-26T20:08:18+00:00Added an answer on May 26, 2026 at 8:08 pm

    You need to do a user.garage.save to save the embedded garage after you populate it.

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

Sidebar

Related Questions

The problem I have multiple groups which specify the relationships of symbols.. for example:
HI, I have 3 tables: Clips , Books and relationships between ClipBook Problem is:
I have a problem with one-to-many relationships. I have the following domain classes: public
I have a problem with one-to-one relationships in the fluent nHibernate. I have the
I've got a problem with UITableViews and many-to-many relationships. I have two classes, A
I have a problem with core data when setting relationships: The code below crashes
I have recently stumbled upon a problem with selecting relationship details from a 1
I have a very big problem. I am making a CRM (Costumer Relationship Management)
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names

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.