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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:39:32+00:00 2026-06-13T22:39:32+00:00

I have gone over the documentation, and I can’t find a specific way to

  • 0

I have gone over the documentation, and I can’t find a specific way to go about this. I have already added some dynamic attributes to a model, and I would like to be able to iterate over all of them.

So, for a concrete example:

class Order
  include Mongoid::Document

  field :status, type: String, default: "pending"
end

And then I do the following:

Order.new(status: "processed", internal_id: "1111") 

And later I want to come back and be able to get a list/array of all the dynamic attributes (in this case, “internal_id” is it).

I’m still digging, but I’d love to hear if anyone else has solved this already.

  • 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-13T22:39:34+00:00Added an answer on June 13, 2026 at 10:39 pm

    Just include something like this in your model:

    module DynamicAttributeSupport
    
      def self.included(base)
        base.send :include, InstanceMethods
      end
    
      module InstanceMethods
        def dynamic_attributes
          attributes.keys - _protected_attributes[:default].to_a - fields.keys
        end
    
        def static_attributes
          fields.keys - dynamic_attributes
        end
      end
    
    end
    

    and here is a spec to go with it:

    require 'spec_helper'
    
    describe "dynamic attributes" do
    
      class DynamicAttributeModel
        include Mongoid::Document
        include DynamicAttributeSupport
        field :defined_field, type: String
      end
    
      it "provides dynamic_attribute helper" do
        d = DynamicAttributeModel.new(age: 45, defined_field: 'George')
        d.dynamic_attributes.should == ['age']
      end
    
      it "has static attributes" do
        d = DynamicAttributeModel.new(foo: 'bar')
        d.static_attributes.should include('defined_field')
        d.static_attributes.should_not include('foo')
      end
    
      it "allows creation with dynamic attributes" do
        d = DynamicAttributeModel.create(age: 99, blood_type: 'A')
        d = DynamicAttributeModel.find(d.id)
        d.age.should == 99
        d.blood_type.should == 'A'
        d.dynamic_attributes.should == ['age', 'blood_type']
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that I have gone over at least 30 times by
I am sure someone has gone over this but I have had no luck
Ok I am getting this FC error. I have gone over the code three
I've seen this question asked numerous times and have gone over my code over
I've gone over what feels like every post about this method on SO. And
I have gone over this over and over, and cannot for the life of
I have gone over this for too long without any results, why isn't this
I have gone through this to change the color of my title bar. I'm
I have gone through this page... http://www.php.net/manual/en/function.hash.php MD5 is 32 characters long while sha1
I have gone through the fadein/fadeout tutorial that is on this page: For 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.