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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:18:36+00:00 2026-06-04T21:18:36+00:00

Im newbee in mongoid and I have two basic (I think) questions. Whats best

  • 0

Im newbee in mongoid and I have two basic (I think) questions. Whats best way to store array of references in Mongoid. Here is a short example what I need (simple voting):

{
  "_id" : ObjectId("postid"),
  "title": "Dummy title",
  "text": "Dummy text",
  "positive_voters": [{"_id": ObjectId("user1id")}, "_id": ObjectId("user2id")],
  "negative_voters": [{"_id": ObjectId("user3id")}]
}

And its a right way?

class Person
  include Mongoid::Document
  field :title, type: String
  field :text, type: String

  embeds_many :users, as: :positive_voters
  embeds_many :users, as: :negative_voters
end

Or its wrong?

Also Im not sure, maybe its a bad document structure for this situation? How can i gracefully get if user already voted and dont allow users vote twice? Maybe I should use another structure of document?

  • 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-04T21:18:38+00:00Added an answer on June 4, 2026 at 9:18 pm

    Rather than embeds_many you can go for has_many because you just want to save the reference of voters in the document rather than saving whole user document in person

    class Person
        include Mongoid::Document
        field :title, type: String
        field :text, type: String
    
        has_many :users, as: :positive_voters
        has_many :users, as: :negative_voters
    
        validate :unique_user
    
        def unique_user
           return self.positive_voter_ids.include?(new_user.id) || self.negative_voter_ids.include?(new_user.id)         
        end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am newbee with PHP and MySQL and need help... I have two tables
Newbie here, I have a struct for a word, which contains a char array
NEWBIE logger question here: What's the best way to log output to a development
I am newbee to jquery and have some work assigned to me to complete
Still new to the memory management in iPhone apps, I have a newbee question.
Newbee to Android development. I am developing a application, which have three different tabs.
I'm trying to figure out the best way to manage some enthusiastic adult students
Newbie question here but for some reason I cant figure this out. I have
Newbie asking for help: I have an array of numbers that needs to be
So here is the deal. I have a div I want to show the

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.