Filter activeadmin with hstore

前端 未结 2 1591
萌比男神i
萌比男神i 2021-01-24 04:10

I would like to use activeadmin filters with hstore:

In model I have got column amenities with Room.

I would like to do sth like this:

fil

相关标签:
2条回答
  • 2021-01-24 05:06

    You could just create a Formtastic custom input for the HStore Datatype. If you don't want the Hstore values to be editable this should be sufficient (you could additionally set the the input field to read-only with input_html_options):

    class HstoreInput < Formtastic::Inputs::StringInput
    
    end
    

    This will break the attribute values on write though.

    0 讨论(0)
  • 2021-01-24 05:06

    With latest activeadmin (which uses ransack instead of meta_search) it's possible to define a custom ransacker for hstore field in a model:

    class Room < ActiveRecord::Base
      store_accessor :options, :amenities
    
      ransacker :amenities do |parent|
        Arel::Nodes::InfixOperation.new('->', parent.table[:options], 'amenities')
      end
    end
    

    Then it can be used in activeadmin for filtering:

    ActiveAdmin.register Room do
      filter :amenities_eq, label: 'Amenities', as: :select # ...
    end
    
    0 讨论(0)
提交回复
热议问题