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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:47:04+00:00 2026-05-22T00:47:04+00:00

I have a model User where I make heavy use of dynamic attributes. When

  • 0

I have a model User where I make heavy use of dynamic attributes.
When I am displaying the user show, I skip the first set of attributes to skip the id etc.
Problem is that the attributes are sorted by alphabetical order, not by order of creation
so if for example I create a users as:


MONGODB startuplab_co_development['users'].insert([{"provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "_id"=>BSON::ObjectId('4dc5ad606acb26049e000002'), "email"=>"dan@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio"}])

Even though the insertion second attribute is uid, when I retrieve the keys they are sorted alphabetically.


%h1 User
%ul
-keys = @user.attributes.keys[3..-1]
- keys.each do |key|
  %li
    %span
      %strong= "#{key.capitalize()}:"
    %span= "#{@user[key]}"

So for instance this will print UID as the last attribute, since they were sorted.


First_name: Daniel
Last_name: Palacio
Name: Daniel Palacio
Provider: google
Uid: https://www.google.com/accounts/o8/id?id=AItOawl_oas_8jcY1VSTQchsc 

Is there anyway that I can make sure the attributes position stay in the order of insertion ?

Here is the chain of events where attributes get sorted

  1. After creation uid is the 3rd attribute


ruby-1.9.2-p0 > user = User.first
 => _id: 4dc5c5946acb26049e000005, _type: nil, _id: BSON::ObjectId('4dc5c5946acb26049e000005'), provider: "google", uid: "https://www.google.com/accounts/o8/id?id=AItOawl_oas_", admin: nil, email: "danpal@gmail.com", first_name: "Daniel", last_name: "Palacio", name: "Daniel Palacio"> 

ruby-1.9.2-p0 > user.attributes
 => {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio"} 

  1. Now we update the admin attribute and save it, uid is still the 3rd attribute


ruby-1.9.2-p0 > user.update_attributes(:admin => true)
 => true 
ruby-1.9.2-p0 > user.attributes
 => {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio", "admin"=>true} 

ruby-1.9.2-p0 > user.save
 => true 

ruby-1.9.2-p0 > user.attributes
 => {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_", "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio", "admin"=>true} 

  1. Know we retrieve the object from the database again, uid is now the last attribute, and they have been sorted alphabetically.


ruby-1.9.2-p0 > user = User.first
 => # 
ruby-1.9.2-p0 > user.attributes
 => {"_id"=>BSON::ObjectId('4dc5c5946acb26049e000005'), "admin"=>true, "email"=>"danpal@gmail.com", "first_name"=>"Daniel", "last_name"=>"Palacio", "name"=>"Daniel Palacio", "provider"=>"google", "uid"=>"https://www.google.com/accounts/o8/id?id=AItOawl_oas_"} 

  • 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-22T00:47:04+00:00Added an answer on May 22, 2026 at 12:47 am

    Ok here is the answer:

    Basically during an update if the Document allocated space is not sufficient( Eg. becouse the update add’s a new field or grows an existing field), the document will be moved and the fields are reordered(alphanumerically).

    From the MOngoDB docs:
    http://www.mongodb.org/display/DOCS/Updating

    Field (re)order

    During an update the field order may
    be changed. There is no guarantee that
    the field order will be consistent, or
    the same, after an update. At the
    moment, if the update can be applied
    in place then the order will be the
    same (with additions applied at the
    end), but if a move is required for
    the document (if the currently
    allocated space is not sufficient for
    the update) then the fields will be
    reordered (alphanumerically).

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

Sidebar

Related Questions

I have a user model on which I check to make sure that the
I have a User model, and use an acts_as_authentic (from authlogic) on it. My
In my app, I have a User model, which includes a number of attributes
I have a User model that has_many :posts. If I wanted to make a
The problem is the default User model does not have some very useful options
If I have a fairly complex User model that I would like to use
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
Say I have a model called User that has the following parameters: favorite_color, favorite_animal,
I have a model Foo which have a ForeignKey to the User model. Later,

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.