문제

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.

도움이 되었습니까?

해결책

You want the empty? method:

query = "SELECT * FROM albums WHERE artist = 'John';"
empty = DB.fetch(query).empty?
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top