rubysqlitesequelramaze

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


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.


Solution

  • 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.