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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:44:30+00:00 2026-06-06T08:44:30+00:00

If you supply Mongo with a hash that uses symbols as keys and save

  • 0

If you supply Mongo with a hash that uses symbols as keys and save the document, it will ‘stringify’ it, meaning the keys will be converted to strings. To summarize:

condition:                       hash keys will be:
----------                       ------------------
after initializing a document    symbols or strings
after saving a document          strings
after fetching a document        strings

This ‘asymmetry’ has led to some ugliness in my tests. I would like to be able to ‘rely on’ the keys always being strings – and not worry about if the document has just been initialized or not.

What are one or more elegant ways to avoid this?

Note: In my case, I’m using Mongoid, but I don’t think this question is necessarily Mongoid specific. It probably applies to any Rails project that uses MongoDB.

  • 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-06T08:44:32+00:00Added an answer on June 6, 2026 at 8:44 am

    Something along these lines could work. Basically this code redefines Mongoid’s field macro (its setter).

    require 'mongoid'
    
    module Stringifier
      def field name, args = {}
        super # call mongoid implementation
    
        define_method "#{name}=" do |val|
          val.stringify_keys! if val && val.respond_to?(:stringify_keys!)
          super(val)
        end
      end
    end
    
    class Foo
      include Mongoid::Document
      extend Stringifier
    
      field :subhash, type: Hash
    end
    
    f = Foo.new
    f.subhash = {a: 1, b: 2}
    
    puts f.subhash
    # >> {"a"=>1, "b"=>2}
    

    This may not be the cleanest implementation, but you get the idea.

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

Sidebar

Related Questions

We have a web page that uses Process.Start to run msbuild, and we supply
Is there a switch I can supply to cl.exe that will enable a warning
Our Client requires that we supply Widgits for their site. They want to link
Background: We're building an application that allows our customers to supply data in a
A supply farm can have a transportation document. If present it can be one
We have a number of customers in which we supply a database that was
How to supply custom key as dictionary keys? When i execute the code: I
Layer-hosting NSViews (so NSViews that you supply a CALayer instance for and set it
A requirement for a 3rd party config is that i supply it with: 1.
In app.config, I supply a cycle string value: Monday, Tuesday, ..., Daily. From that

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.