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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:17:43+00:00 2026-05-30T09:17:43+00:00

So, I am having some issues with user authentication in embedded documents. I have

  • 0

So, I am having some issues with user authentication in embedded documents. I have two documents, one embedded in the other. A business has many members. The models look like this:

class Member
  include Mongoid::Document

  field :username,        type: String
  field :password,           type: String
  embedded_in :business

  validates :username,  :presence => true, :uniqueness => true, :length => 5..60
end

class Business
  include Mongoid::Document

  field :name,            type: String
  embeds_many :members
end

The problem is that it isn’t validating the username’s uniqueness in each model. When I save a member within a business, I can save a thousand of the same name. This of course is not going to work for a good authentication system. I am using Mongoid 2, Rails 3, and Ruby 1.9

  • 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-30T09:17:44+00:00Added an answer on May 30, 2026 at 9:17 am

    This is a normal behavior when using embedded documents as explained here: MongoID validation

    validates_uniqueness_of

    Validate that the field is unique in the database: Note that for
    embedded documents, this will only check that the field is unique
    within the context of the parent document, not the entire database.

    I think you want to try to create an Index in the username field that would ensure uniqueness among all the objects of that collection. Something like this:

    ensureIndex({username:1},{unique:true}); 
    

    EDIT: If you want Mongo to throw exception if a document with the same index value exists, you must avoid Mongo to do the “fire and forget” pattern. This means that the database will not wait for a response when you perform an update/write operation on a document.

    And you want to pass this parameter: safe:true. By doing so Mongo should raise an exception if for any reason the document can’t be inserted.

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

Sidebar

Related Questions

I'm having some issues with dijit.form.Select items. In a page i have two of
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
I was having some issues the other day with my interface lagging and after
I am having some issues when updating an object that has a nested attribute.
Ok I am still having some issues with this code below. I have received
I'm having some issues using the following code on user input: htmlentities($string, ENT_COMPAT, 'UTF-8');
I'm having some issues implementing a TimePicker in my application that allows the user
I'm having some issues with HybridAuth when I need to redirect a non-registered user
I'm having some issues with jenkins and gerrit. We have some jobs that verifies
Having some issues getting my head around the differences between UTF-8, UTF-16, ASCII and

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.