简体   繁体   中英

Custom Ransacker to ignore dash in database

I'm trying to customize Ransack to allow searching the text field and possibly having ransack ignore the dash character.

EXAMPLE:

If I type this in the search box:

123456

The Ransack query will not find this record in the DB:

123-456

Right now if I don't type the dash the record will not be found. I understand why it's trying to match the DB record and it's obviously not finding it because the dash is required to find that record.

Is it possible to customize Ransack so that the search will still find the record without the dash in the params?

Looks like creating a custom Ransacker does the trick here.

ransacker :number do
  Arel.sql("translate(number, '-', '')")
end

This will remove the dashes from the database representation of the data when searching with Ransack.

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