1

I'm in the Rails console and I want to generate a list of user names that have a trailing whitespace in them. I was thinking that the syntax would look like this, but it didn't work. Any change a better programmer than me can point out what I'm doing wrong?

> User.name.where("% ")

3 Answers 3

3

Don't know if you're using MySQL, but an approach would be:

User.where("name LIKE '% '")

You may change this according to your database. This is kinda slow, though.

Sign up to request clarification or add additional context in comments.

Comments

1

One way is

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

but it may not be as efficient as the MySQL version.

Another possibility is to use a named scope to hide the ugly SQL:

  named_scope :all_digits, lambda { |regex_str|
    { :condition => [" invoice_number REGEXP '?' " , regex_str] }
  }

Then you have Job.all_digits.

Answer taken from How to specify Ruby regex when using Active Record in Rails?

You can have

regex_str = "\w+\s+$"

Thanks

Comments

0

Here's what I went with:

User.all.select { |c| c.name.end_with?(" ") }

This got me the list I needed.

It's based on Paritosh's first answer. I'm making his a the canonical answer because I think it's a better resource in general. My solution only helps me, but his has a lot of strategies that would be helpful.

1 Comment

You should use shadowmaru method, your way will load all Users into object and parse (memory usage, slow speed), his way all of the work is done on the sql server side (glorious and fast)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.