title: ActiveAdmin/Formtastic custom input (JSON)
body: <p>I have a json column in my database (actually jsonb, but that doesn't matter here) table <strong>items</strong> of only plain </p>
  
  <pre><code>{
   "key1":"value1",
   "key2":"value2"
  } 
  </code></pre>
  
  <p>structure. No nesting.</p>
  
  <p>The "keys" are actually foreign key-like integers pointing at <strong>properties</strong> table, which looks like</p>
  
  <pre><code>id | name
  -----------
  1  | Height
  2  | Weight
  3  | Color
  </code></pre>
  
  <p>etc.</p>
  
  <p>I'm using ActiveAdmin for administration and want to have a custom input on editing an <em>Item</em> composed of a <em>select input</em> and a <em>text field</em>, which will allow me to:</p>
  
  <ul>
  <li>select a property from the properties table OR input a new property, and then</li>
  <li>type a JSON into the text field.</li>
  </ul>
  
  <p>Can't figure out how though.</p>
  
  <hr>
  
  <p>What have I tried and what my problems are?</p>
  
  <p>I have created a custom input in app/inputs/property_input.rb:</p>
  
  <pre><code>class PropertyInput
      include Formtastic::Inputs::Base
      include Formtastic::Inputs::Base::Collections
  
      def to_html
          input_wrapping do
              label_html &lt;&lt;
              select_html &lt;&lt;
              text_html    
          end
      end
  
  protected
  
      def select_html
          builder.select(input_name, collection, input_options, input_html_options)
      end
  
          include Formtastic::Inputs::Base::Stringish
          include Formtastic::Inputs::Base::Placeholder
  
      def text_html
          builder.text_field(input_name)
      end
  
  end
  </code></pre>
  
  <p>Calling it as below from admin/item.rb</p>
  
  <pre><code>f.input :property, as: :property, collection: ItemAttribute.all
  </code></pre>
  
  <hr>
  
  <p>My problem is:</p>
  
  <p>Right now, the select shows correctly the name of the property, but the text field shows just id of the same property. How to decouple these two fields and force the text field to save and read from the Item jsonb column?</p>
  
  <p>Maybe it is important to say that I do <em>not</em> use ActiveRecord associations between Item and Property models in this case as I'm not sure if it even works with the jsonb column holding "foreign keys".  </p>