0

In the user table I have a name field, where some records are names, and some are emails. I know, bad approach. That aside. I'd like to write a named scope that returns only email records.

I've tried adapting a query that does this query, and failed.

MYSQL:

SELECT NAME
FROM USER
WHERE NAME LIKE '%@%.%'

User model:

scope :user_email, :conditions => ["name like '%@%.%'"]

Any help would be appreciated.

jahrichie
  • 1,215
  • 3
  • 17
  • 26

2 Answers2

1

Try this:

scope :user_email, :conditions => ["name LIKE ?", '%@%.%']

You should provide string conditions in this format to avoid possible sql injection problems.

atmaish
  • 2,495
  • 3
  • 22
  • 25
0

Try this:

 named_scope :user_email, :conditions => ["name REGEXP ?", /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i]
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44