Search code examples
rubyrubygemssequelroda

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 is more than one item, it returns an array but when there's only one, it returns a Model item.

For example, 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 10 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.


Solution

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

    Pizza.first(1)