Rails gem / plugin for dynamic custom fields in the model

Is there any gem / plugin for ruby ​​on rails that makes it possible to define custom fields in the model at runtime, without having to change the model itself for every other field .

I am looking for something like a Redmine acts_as_customizable plugin that is packaged like a gem that can be used in rails mode, i.e.

gem 'gemname' rails g something rails db:migrate class Model < ActiveRecord::Base acts_as_something end 

Here are the CustomField and CustomValue classes used in Redmin.

Edit:

Since my question is not clear, I am adding a short usage example that explains my need better:

I want users to be able to create their own forms and collect the data presented on these forms. An important decision is to design how these custom dynamic recordings are saved and accessible.

Taken from here , in this article we come up with a problem with different ideas, but everyone has flaws. For this reason, I ask if the problem was touched on in some stone, and there is no need to rethink the whole problem.

+7
source share
4 answers

I am afraid that it may be difficult and difficult to do this in ActiveRecoand (usually in a standard relational database). Take a look at http://mongoid.org/docs/documents/dynamic.html - this mechanism uses the nosql function.

You can also try the following trick:

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

2 / After loading, a record from the database performs some metaprogramming and, for example, defines the appropriate methods in a single record class (suppose that user fields are saved and serialized in the custom_fields column):

 after_initialize :define_custom_methods # ..or other the most convinient callback def define_custom_methods # this trick will open record 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 
+3
source

I don't know about the gem that does this, but serialize works pretty well and it is built-in. You get a NoSQL-ish document store supported by JSON / YAML.

If you allow the user to create a custom form, you can pass nested arrays and so on directly to the attribute. However, if you need to check the structure, you yourself.

+5
source

With rails 3.2 you can use the store method. Just include the following in your model:

 store :properties, accessors: [:property1, :property2, :property3...] 

You only need to change your model once (add the properties field to the db table). You can add additional properties later without changing the schema.

How it works by serializing a hash of properties in YAML and storing it in the database. It is suitable for most cases, but not for using these values ​​in db queries later.

+3
source

I don’t know the gem, but this can be done by creating a table called custom_fields with a column of names and, possibly, a data type column if you want to limit the fields by data type. Then you create a connection table for the custom field in the desired table and value and perform all the necessary checks.

0
source

All Articles