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

  • Home
  • SEARCH
  • 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 3450562
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:03:51+00:00 2026-05-18T09:03:51+00:00

I have a question regarding mongoid id storage for references_many. Suppose I have the

  • 0

I have a question regarding mongoid id storage for references_many.

Suppose I have the following classes:

class A
  include Mongoid::Document
  field :name

  attr_accessible :name, :b_ids
  references_many :bs, :stored_as :array, :inverse_of :a
end

class B
  include Mongoid::Document
  field :name

  attr_accessible :name, :a_id
  referenced_in :a
end

If I do the following:

a = A.create(:name => "a1")
b = B.create(:name => "b1")
b.a = a

Should I expect the a.b_ids array to be an array that contains b’s id?

The behaviour I am seeing is that b.a_id contains a’s id, but a.b_ids does not contain b’s id.

Is the id array on A’s side supposed to be manually updated?

BTW, if I do a.bs << b, then a.b_ids gets updated correctly.

  • 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-18T09:03:51+00:00Added an answer on May 18, 2026 at 9:03 am

    To answer my own question, the id arrays are not automatically set at the moment.
    This feature is planned to be included once the refactor branch of mongoid is released.

    This info comes from this thread: http://groups.google.com/group/mongoid/browse_thread/thread/9ac74dc9a08a5fe2/d3a7c2404b67abfa

    Until then, the ids have to be tracked manually.

    An example would be:

    class A
      include Mongoid::Document
      field :name
    
      attr_accessible :name, :b_ids
      references_many :bs, :stored_as :array, :inverse_of :a
    
      def add_b b
        bs << b
        self.save
      end
    
      def remove_b b
        b_ids.delete b.id
        b.save
      end
    end
    
    class B
      include Mongoid::Document
      field :name
    
      attr_accessible :name, :a_id
      referenced_in :a
    end
    
    a = A.create(:name => "a1")
    b = B.create(:name => "b1")
    b.a = a
    a.add_b b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the following code: abstract class a { public static
I'm writing a small webapp in Grails and I have the following question regarding
Following on from my recent question regarding parsing XML files in Java I have
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding macros. How could I cast through macro a template class
I have a question regarding inheritance in Java. If I have this base class
I have a question regarding type conversion in Java as follows: suppose I have
I have a question regarding AI. Suppose I have a tank, and in my
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION

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.