Trip
Trip

Reputation: 27114

Can you do a where find with a regular expression in rails?

I'm trying to find objects that begin with a number.

Syntactically this is off. But I would like to do something like this :

Object.where([name LIKE ?', /[1-9]/])

If that isn't possible, how do you suppose the best way to find all objects that start with a number?

Upvotes: 2

Views: 438

Answers (2)

Lee Jarvis
Lee Jarvis

Reputation: 16241

Select values that begin with a number

Object.where(['name REGEXP ?', '^[0-9]'])

Upvotes: 2

Omar Qureshi
Omar Qureshi

Reputation: 9093

You can use rlike / regexp i would think. Though it's not portable across to other databases.

http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp

Object.where(['name rlike ?', '^[\d]'])

Upvotes: 2

Related Questions