"No valid predicate" error when upgraded from Meta_search to Ransack?

1.7k views Asked by At

I've got an ActiveRecord model with a method defined like this:

def state   

  if deleted?
    :deleted    
  else
    :expired
  end

end

The 'search_method' is defined in the model as:

search_method :state

In the view:

= form.select :state, { :expired => 'Expired', :deleted => 'Deleted' }.invert, :include_blank => 'All'

With Meta_search, this method was working fine. But when I replaced the gem with Ransack, I get: ArgumentError in Sample Controller No valid predicate for state.

I'm following this behavior from meta_search search_methods, so I might be taking the wrong approach. Any one help me, please?

1

There are 1 answers

0
allielarson1212 On

Ransack uses a predicate added on to the end of the field you're searching for to indicate how to search for it. If you have an attribute of :state on your model, you could search for states that match to 'expired' or 'deleted' using form.select :state_match, where the _match says to match records with state of whatever you selected. (Things like _cont (contains) or _start (starts with)).

Ransack makes SQL queries to find records, so if you had an attribute on your model like :deleted_at you could search by presence or lack-of. It won't look at methods, just the information stored in the db.

More predicates and info are here: https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching