Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your model, you can dynamically assign attributes from the keys of the serialized Hash, or Array. I've done something similar so am munging my code for a few examples below. Basically , by dynamically creating virtual attributes, using a DSL like Formtastic becomes much easier.</p> <p>With serialized Arrays : </p> <pre><code> class Info &lt; ActiveRecord::Base serialize :exhibition, Array serialize :contest, Array after_initialize :explode_exhibition after_initialize :explode_contest before_save :implode_exhibition before_save :implode_contest private def explode_exhibition() exhibition.each_with_index do |v,i| self.class.send(:attr_accessor, "@exhibition_#{i}") self.instance_variable_set("@exhibition_#{i}", v) end end def explode_contest() contest.each_with_index do |v,i| self.class.send(:attr_accessor, "@contest_#{i}") self.instance_variable_set("@contest_#{i}", v) end end def implode_exhibition() exhibition = [] self.methods.grep(/exhibition_/).each do |method_name| exhibition &lt;&lt; self.instance_variable_get("@#{method_name}", v) end end def implode_contest() contest = [] self.methods.grep(/contest_/).each do |method_name| contest &lt;&lt; self.instance_variable_get("@#{method_name}", v) end end end </code></pre> <p>With serialized Hashes : </p> <pre><code> class Info &lt; ActiveRecord::Base serialize :exhibition, Hash serialize :contest, Hash after_initialize :explode_exhibition after_initialize :explode_contest before_save :implode_exhibition before_save :implode_contest private def explode_exhibition() exhibition.each do |k,v| self.class.send(:attr_accessor, "@exhibition_#{k}") self.instance_variable_set("@exhibition_#{k}", v) end end def explode_contest() contest.each do |k,v| self.class.send(:attr_accessor, "@contest_#{k}") self.instance_variable_set("@contest_#{k}", v) end end def implode_exhibition() exhibition.each do |k,v| exhibition[k] = self.instance_variable_get("@exhibition_#{k}", v) end end def implode_contest() contest.each do |k,v| contest[k] = self.instance_variable_get("@contest_#{k}", v) end end end </code></pre> <p>And then in Formtastic you can access the attributes and build a form dynamically for each virtual attribute. Note the regex, can use some iteration, as it currently will only match self.exhibition_0 through self.exhibition_9, but it also excludes any setter methods by not matching methods like self.exhibition_0=</p> <pre><code> f.object.methods.grep(/^exhibition_.*?(?&lt;!=)$/).each do |method_name| f.input method_name end </code></pre> <p>I haven't fully tested this munged code, but the majority of this I have working in a few of my own classesn. Also, there may be a need to dynamically create the setter methods as well. Let me test this out a bit, and improve on this idea.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload