简体   繁体   中英

“No valid predicate” error when upgraded from Meta_search to Ransack?

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' 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM