我有兩個模型Product
和RetailerProduct
。我想建立一個one-to-one
這個模型之間的關係。關係邏輯很複雜,它會吃掉所有的表現。所以,我們決定在批處理作業中填寫retailer_product_id
products
表。從模型屬性的兩側刪除比較時的前導零
問題是,我們有一個條件,我們必須匹配products.matching_data
與retailer_products.matching_code
。這兩個表都在某些情況下包含前導零,我們希望忽略前導零。出於某種原因,我無法更改列的數據類型。
問題是下面的查詢只從一側刪除零。我想在兩邊被刪除。
retailer_products = RetailerProduct.find(11223) // just initialising retailer_product variable
Product.where matching_data: retailer_products.matching_code.sub(/^[0:]*/,"")
例如,上面的查詢將失敗,下面的數據。它只會匹配一行,但邏輯上這兩行匹配。
Retailer_product表
retailer_products | matching_code
1 | "0123a"
2 | "139"
產品表
products | matching_data
1 | "123a"
2 | "0139"
爲什麼不修剪從數據本身的前導零,並確保東西留在未來的道路? –
,因爲不幸的是,前導零在其他條件和其他模型中很重要。 –