djoll
djoll

Reputation: 1229

Find records where field value is all LOWERCASE

In a Rails 3.2 app I have a Tag model and want to find all records where the value in the name:string field is all lowercase.

Thus the activerecord query (on Postgres) would return Tag(id: 1, name: 'test') but not Tag(id:2, name: 'Test').

I'm sure there's a straightforward way to do this but I haven't been able to produce a working query!

Upvotes: 7

Views: 2981

Answers (2)

Brad Werth
Brad Werth

Reputation: 17647

You can use a regular expression to search for all lowercase, like:

Tag.where('name ~ :regex', regex: '^[a-z]+$')

Upvotes: 5

Mischa
Mischa

Reputation: 43298

This should work:

Tag.where('name = lower(name)')

If name equals lower(name) it means that name is lowercase.

Upvotes: 12

Related Questions