Custom filter/search/column? (particularly: using "having")

Skip to first unread message

Jeff Tratner

Nov 3, 2012, 9:24:35 PM11/3/12
I'd like to add a custom search or filter method.  Basically I want to be able to do a search or filter on a calculated column. if I were going to write it as a direct query, I would do something like:

MyModel.having(["SUM(payments.payment_amount) >= ?", 100.0)

and bonus points for being able to use a custom select like["SUM(payments.payment_amount) AS payment_amount, mymodels.*"])

I'm just having a bit of trouble figuring out either:
1 - if this is already a part of railsadmin
2 - what elements need to be monkey patched or changed to hook in a search attribute like this.  Any suggestions?

Just getting pointed to where to look to hack around with this would be great...I admit the RailsAdmin code is a bit complicated for me.

I was thinking it would be simple to (at the very least) incorporate having query by patching `all` to recognize `having` as an option. (since all gets called to scope all other queries, not super difficult)

module RailsAdmin
  module Adapters
    module ActiveRecord
      # monkey patch on RailsAdmin's ActiveRecord adapter to allow it to take in
      # custom `having` parameters
      def all_with_having(options = {}, scope = nil)
        scope = all_without_having(options, scope)
        scope.having(options[:having]) if options[:having]
      alias_method_chain :all, :having

so the next step is just to figure out adapt RailsAdmin's already existing search methods to do this.

Any suggestions?



Ricardo Paiva

Mar 28, 2019, 9:34:54 PM3/28/19
to rails_admin
Hi Jeff, did you manage to do this ?
Reply all
Reply to author
0 new messages