How to specify Ruby regex when using Active Record in Rails?(在 Rails 中使用 Active Record 时如何指定 Ruby 正则表达式?)

本文介绍了在 Rails 中使用 Active Record 时如何指定 Ruby 正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要获得所有 invoice_number 是纯数字的工作,我要做的:

To get all jobs which invoice_number is a pure number I do:

Job.where("invoice_number REGEXP '^[[:digit:]]+$'")

是否可以通过在 Ruby 而不是 MySQL 中指定正则表达式来做同样的事情?

Is it possible to do the same by specifying the regex in Ruby rather than MySQL ?

推荐答案

一种方法是

Job.all.select{|j| j =~ /^d+$