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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:06:09+00:00 2026-06-06T16:06:09+00:00

i have the following code in the rails company model: class Company include Mongoid::Document

  • 0

i have the following code in the rails company model:

class Company
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name,      type: String
  ...
  has_and_belongs_to_many :users
end

User model:

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include ActiveModel::SecurePassword

  field :email,           type: String
  ...
  has_and_belongs_to_many :companies
end

There is a company record in the database, and a user record and they are associated. For some reason, the following code does NOT work:

c = Company.first
c.users # returns empty array

similarly, the followign code does not work:

u = User.first
u.companies

But the following code DOES work:

c = Company.first
user = User.find c.user_ids.first

and the following code also works:

u = User.first
company = Company.find u.company_ids.first

so if i try to access users from the company.users, it does not work, but the user_ids array does have a list of user ids, and when i try to access the users from this list, it works. How can i fix this issue?

i am using rails 3.2.5 and mongoid 3.0.0.rc

  • 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-06T16:06:10+00:00Added an answer on June 6, 2026 at 4:06 pm

    I had exactly the same issue 😉
    Make sure you’re using mongodb version > 2.0.0, for more details see: http://mongoid.org/en/mongoid/docs/installation.html#installation

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

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
In my Rails 3 project I have the following code for My controller: class
I have the following code in my Rails app: <h2>Budget (optional)</h2> <select id=trip_currency name=trip[currency]>
Hi I have the following code, that checks for Rails model status var intervalCall
I have the following code in a rails model: foo = Food.find(...) foo.with_lock do
We have the following code working for a complex rails form with checkboxes. I'm
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
In my Rails 3.2.3 application I have the following code in the application.html.erb to
I'm having an issue with rails involving javascript. Basically, I have the following code:
I have the simple following code, which is working in a ruby (not 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.