简体   繁体   中英

Rails ActiveRecord - find by substring of another string

id | name
1  | jack
2  | tomas

I want to find a row if the name is a substring of Ttomas . So the result should be

id | name
2  | tomas

Is this possible?

Yes, this is possible in Ruby on Rails and SQL. Depends a bit on the database you use, but something like this should work:

Modelname.where("? LIKE CONCAT('%', name, '%')", 'Ttomas')

It's possible.

The brute-force way would be to iterate through all your users and check if name is a substring. But this horribly inefficient.

If you want to utilize SQL lookups, you need to look into gems like pg_search or elasticsearch for full search functionality.

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