B A B A - 5 months ago 6
Ruby Question

How to get single result as an array rather than a Model in Sequel ORM

I am working on a simple application using Roda Ruby gem and Sequel. I want Sequel to return a single result as a array rather than a Model object type. When there are more than one items, it returns them as a array but when there's only one, it returns as a Model item. eg,
in app.rb

get 'pizza' do
@pizza = Pizza.first #=> returns object type Pizza, i want this to be a array as well
end

get 'pizzas' do
@pizzas = Pizza.first(10) #=> returns an array of the first 20 results.
end


If I could get a array instead of an Object type in the first case, I could use the same template for both. Otherwise it will be a bit of a hassle. Any help would be appreciated.

Answer

Just use first with an argument to trigger the hash syntax. This works even when the argument is just 1:

Pizza.first(1)
Comments