ruby-on-railsactiverecordruby-on-rails-pluginsdynamic-data

Rails gem/plugin for dynamic custom fields in model


Is there any gem/plugin for ruby on rails which gives the ability to define custom fields in a model at runtime with no need to change the model itself for every different field.

I'm looking for something like Redmine acts_as_customizable plugin which is packaged as a gem usable in the rails way, i.e.

gem 'gemname'
rails g something
rails db:migrate

class Model < ActiveRecord::Base
  acts_as_something
end

Here are the CustomField and the CustomValue classes used in Redmine.

Edit:

Since my question is not clear I add a brief use case which explains my need better:

I want users to be able to design their own forms, and collect data submitted on those forms. An important decision is the design of how these custom dynamic records are stored and accessed.

Taken from here, in this article approach the problem with different ideas, but they all have drawbacks. For this reason I'm asking if the issue has been approached in some gem with no need to rethink the whole problem.


Solution

  • I'm afraid it could be tricky and complicated to do it in ActiveRecoand (generally in standard relational database). Take a look at http://mongoid.org/docs/documents/dynamic.html - this mechanism is using nosql feature.

    You can also may try the following trick:

    1/ Serialize a hash with your custom fields in the database column, for example { :foo => 'bar', :fiz => 'biz' }

    2/ After load a record from database do some metaprogramming and define corresponding methods on the record's singleton class, for instance (assume that custom fields are stored and serialized in custom_fields column):

    after_initialize :define_custom_methods
    # ..or other the most convinient callback
    
    def define_custom_methods
      # this trick will open record's singleton class
      singleton_class = (class << self; self; end)
      # iterate through custom values and define dynamic methods
      custom_fields.each_with_key do |key, value|
        singleton_class.send(:define_method, key) do
          value
        end
      end
    end