User specified dynamic model fields in Rails

Does anyone know of a gem or a good implementation of allowing the user to add fields to a model?


  • How to manage non-autoincrement primary key in Rails?
  • Accessing a join-model attribute stored in a join table created with #create_join_table
  • How can I use ActiveRecord on a database that has a column named 'valid'? (DangerousAttributeError)
  • What are the Rails's ActiveRecord database gems on JRuby
  • Rails Parellel gem: ActiveRecord::StatementInvalid: Mysql2::Error: MySQL server has gone away
  • Ruby on Rails query yielding unexpected results
  • User would like to add a “internal notes” field to the contact model. In the interface they would just select “New field” > “Type: Text”


  • How does Rails CSRF protection work?
  • Private pub issue in rails application
  • APNS Houston gem moving to config files
  • Authorizing Namespaced and Nested controllers using CanCan
  • Bundler in production not ignoring the development and test gems
  • Validate non-model field
  • 2 Solutions collect form web for “User specified dynamic model fields in Rails”

    I’m sorry I don’t know of any plugin to do that. But I have an implementation suggestion.

    The idea is to add a “DynamicField” model which would be a has_many relation to the Contact model.
    When you have a method missing in the Contact model, you check if there’s a dynamic field to retrieve it if that’s the case.

    class DynamicField < ActiveRecord::Base
        belongs_to :contact
    class Contact < ActiveRecord::Base
        has_many :dynamic_fields
        def method_missing(sym, *args, &block)
                field = dynamic_fields.find_by_name(sym)
            raise ActiveRecord::NoMethodError if field.nil?

    You will need to add a regex if you want to add virtual attributes with the attribute= method (detecting the presence of a “=” and doing an update instead of only getting the value).
    But you already have here the idea.

    When the method doesn’t exists, we check the dynamic fields if there is one with the same name.
    If there isn’t (field.nil?), we raise a NoMethodError. Otherwise, we return it.

    So you could get a list of all your fields with the following :


    And retrieve a specific one with the following :


    Here’s another way of doing it.

    Disclaimer: This method is not advised because of it’s potential for abuse. In fact, it should only be accessible for Administrators of your site.

    class MyModel < ActiveRecord::Base
      def self.add_column(name, type, args= {})
        ActiveRecord::Migration.add_column table_name, name, type, args
    Ruby is the best programming language in the world - Ruby on Rails.