5

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?

  • I think you need to apply the predicate to the field like `state_start`. Have a look at this issue https://github.com/activerecord-hackery/ransack/issues/22 – phoet Jan 08 '14 at 13:11

1 Answers1

0

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