This question is quite simple, but I have come across a problem several times.
Let's say you do something like:
cars = Vehicle.find_by_num_wheels(4)
cars.each do |c|
puts "#{c.inspect}"
end
This works fine if cars are an array, but fail if there is only one car in the database. Obviously, I could do something like "if! Cars.length.nil?" or check another way if the car object is an array before calling .each, but it's a bit annoying to do it every time.
Is there something similar to .each that handles this check for you? Or is there an easy way to force the result of a query into an array, regardless of size?
source
share