Dynamic class methods and attributes

I have a class Form that represents a form that a user fills in
The elements on the form are determined through the app and they can be
any type of form field, file, select, text, textarea etc…

a collection of fields on a form

class Form < ActiveRecord::Base
has_many :elements

the individual elements of a form

class Element
belongs_to :form

refers to an instance of a form as filled in by a user

class FormProfile
has_many :form_element_answers
belongs_to :form
belongs_to :user

each individual elements answer by a user

class FormElementAnswer
belongs_to :form_profile

When displaying the form to a user the Form object is found then all of
its elements are looped through and displayed on the page with the right
type of input element.

When they save their answers the controller loops through each object in
params and creates a FormElementAnswer row which corresponds to the
FormProfile for that user.

Now I was wondering if there was a simple way of setting up FormProfile
to somehow dynamically know what elements are on that form. So instead
of looping through params and creating a row for each FormElementAnswer
I could just do FormProfile.new(params[:whatever]) and it takes care of
all the work itself in the model.

This way validation would be on FormProfile and not on each individual

Maybe it would dynamically allocate getters and setters for each element
of the form on FormProfile itself?

Not sure if this clear?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs