我一直在努力解決客戶要求根據列格式過濾掉模型(候選)的問題
我面臨的問題是列輸入 SSN 或 EIN。SSN 的格式是 (xxx-xx-xxxx) EIN 的格式是 (xx-xxxxxxx)
我的候選人表包含欄位 ssn_or_ein ,它需要這 2 個之一。
例如:Candidate111.ssn_or_ein => 111-11-1111Candidate222.ssn_or_ein => 22-2222222
我已經嘗試獲取所有 4000 個帳戶,但我認為這不是開發人員的方法。
我仍在學習 Rails,任何提示都會非常有幫助。
uj5u.com熱心網友回復:
您可以使用類似查詢來執行此操作。把它放在一個范圍內,所以它很容易獲得。
class Candidate < ApplicationRecord
scope with_ein -> { where( "ssn_or_ein like ?", "__-_______" }
scope with_ssn -> { where( "ssn_or_ein like ?", "___-__-____" }
end
但是,如果ssn_or_ein沒有正確索引,這可能會變慢。
考慮將它們存盤在兩個不同的列中。這使得驗證和查詢更簡單。僅當您只需要TIN 納稅人資訊編號時才將它們放在一起。
class Candidate < ApplicationRecord
scope with_ein -> { where.not( ein: nil ) }
scope with_ssn -> { where.not( ssn: nil ) }
EIN_RE = %r{^\d{2}-\d{7}$}
SSN_RE = %r{^\d{3}-\d{2}-\d{4}$}
validates :ein, format: { with: EIN_RE }, allow_nil: true
validates :ssn, format: { with: SSN_RE }, allow_nil: true
def tin
ssn || ein
end
class << self
def find_by_tin(tin)
where( ssn: tin ).or( where(ein: tin) )
end
end
end
我還建議您存盤“標準化”的資料,沒有破折號,只有數字。這更易于使用,并且可以更改可接受的格式而無需更改所有資料。在裝飾器中格式化它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/342928.html
標籤:红宝石轨道 红宝石 ruby-on-rails-5 导轨控制台
上一篇:Ruby:我有一個哈希,keys=str.split(//)和values=這些字符的索引。如果存在超過1個字符,我如何將多個索引添加到值
