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?
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' usingform.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