简体   繁体   English

使用条件在字段上进行ActiveRecord搜索

[英]ActiveRecord search on field with criteria

I'm relatively new to Rails and ActiveRecord. 我是Rails和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). 我将电话号码存储在ActiveRecord字段(phone_number)中,其格式可以为“ +1 928 929 3829”或“ 393 938 0293”或“ 392-937-9283”(您知道了)。

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. 后来,我捕获了一个10位数的电话号码(因此,它将始终像“ 3939380293”),然后尝试将其与我的ActiveRecord电话号码字段匹配。 The operation I want to do is match against the last 10 numeric characters of phone_number. 我要执行的操作与phone_number的最后10个数字字符匹配。

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. 我尝试查看可以在查询中使用的不同SQL类型的查询约束,但是我无法准确地找到所需的内容或使逻辑步入正轨。

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") 1-具有用于电话号码的VARCHAR字段,该字段将以任何格式保存号码(例如:“ + 1 928 929 3829”或“ 393 938 0293”或“ 392-937-9283”)

2 - Have a Activerecord to select the phone number with a regex . 2-具有Activerecord以使用正则表达式选择电话号码。

My most favourite regex writers are rubular , regexper 我最喜欢的正则表达式作者是rubularregexper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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