简体   繁体   中英

ActiveRecord search on field with criteria

I'm relatively new to Rails and ActiveRecord. I'm storing phone numbers in ActiveRecord fields (phone_number) that can be in a number of formats "+1 928 929 3829" or "393 938 0293" or "392-937-9283" (you get the idea).

Later I'm capturing a 10 digit phone number (so it will always be like "3939380293") and then trying to match that to my ActiveRecord phone number field. The operation I want to do is match against the last 10 numeric characters of phone_number.

I've tried looking at the different SQL-type query constraints that you can use in queries, but I'm having trouble finding exactly what I need or making the logic leap.

Can anyone offer some advice here?

I would do followings

1 - having a VARCHAR field for phone number, which will save numbers in any format (Ex: "+1 928 929 3829" or "393 938 0293" or "392-937-9283")

2 - Have a Activerecord to select the phone number with a regex .

My most favourite regex writers are rubular , regexper

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