Question

I am performing a query to a database in ruby using Sequel ORM. The query is performed by:

query = "SELECT * FROM albums WHERE artist = 'John';"
DB.fetch(query)

I would like to check whether the result of the query is empty, i.e. if not entry of the DB matches the query conditions.

I could:

empty = true
DB.fetch(query) do |row|
      empty = false
end

but I would like to know whether there is a direct method to chek whether the query returns no result.

Was it helpful?

Solution

You want the empty? method:

query = "SELECT * FROM albums WHERE artist = 'John';"
empty = DB.fetch(query).empty?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top