B A B A - 1 year ago 90
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

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

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download