I'm trying to add a custom filter field for an hstore
field.
In activeadmin: filter :by_title, label: 'Title', as: :string
In model :
ransacker :by_title do |parent|
Arel::Nodes::InfixOperation.new('->',
parent.table[:title_translations], Arel::Nodes.build_quoted('en'))
end
This code is searching creating the correct query depending on the activeadmin selection (contains, equals, starts_with or ends_with)
I use the hstore to store title for multiple locales. I'd like to search in ALL locale. Not only en
, but fr
, es
, ru
, etc
What I ended up doing was using a custom scope like so:
# Ransackable Scopes
def self.ransackable_scopes(_auth_object = nil)
[:by_title]
end
# My scopes
scope :by_title, ->(search_term) { by_field(search_term, :title) }
scope :by_field, lambda { |search_term, field|
where("#{field}_translations -> :key ILIKE :value", key: :en, value: "%#{search_term}%")
.or(where("#{field}_translations -> :key ILIKE :value", key: :fr, value: "%#{search_term}%"))
.or(where("#{field}_translations -> :key ILIKE :value", key: :de, value: "%#{search_term}%"))
}
It doesn't exactly answer the question, but was a solution that worked for me needs.