0

Im trying to figure out the right way to do this. The idea is to learn/understand how to do searches with partial information. Like if I'm searching 500 usernames for all that have "Test" as part of the username. (examples "Test Same", "Ron Test", etc)

For the following I am in the console testing ways to find data. User.all returns all the data for all users. I want to return all data for users whos username includes "Test".

So i did the following in the console

User.all

returned a array with key/value pairs of all the users and related information. 2 Users had a username that had "Test" as part of the username. So i tried to return just those 2 users like this:

User.where("username LIKE ?", "%Test%")

No luck with that command in the console. All it returns is the following:

#<Sequel::SQLite::Dataset: "SELECT * FROM `users` WHERE (username LIKE '%Test%')">

Just in case it matters, I'm using ruby 1.9.3, and ramaze but I think the answers would still apply using rails.

2 Answers 2

2

I think you are almost there. Sequel returns a DataSet that can be chained to further selection, projection, joins etc. This would build up more SQL, therefore it does not hit the database yet. If you want to go there with the current query you can simply call .all or .first on it to get the result of the actual call. To get the array you just chain your two calls as in

User.where("username LIKE ?", "%Test%").all

It is really that simple.

0
0

Try this in the console...

selected_users = User.where("username LIKE ?", "%Test%")

selected_users.each{|u| p u.username}

... and I think you will be pleasantly surprised. The sequel isn't executed until you attempt to access the records.

1
  • Thanks for your answer. I went with the above from @Patru as that is what I was trying to do. I was just missing the .all chain at the end.
    – jtlindsey
    Jun 21, 2014 at 2:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.