Comparison of Array with Array failed

Hi all,

i got this error when the field serial is empty.

Showing app/views/find/index.rhtml where line #44 raised:

comparison of Array with Array failed

Extracted source (around line #44):

41: <% equipements = Equipement.find(:all,
:conditions => [ 'id != “-1” ’ ])
42: @tab = [["— Choose —", -1]]
43: @liste = @equipements.collect {|elt| [ elt.serial, elt.id ]}
44: @thetab = @tab + @liste.sort
45: %>
46: <%=
47: select(“profile2”, “modele_om”, @thetab) %>

Can you help me ?

Thanks

I don’t know if this is the root or not. Array#sort uses the <=>
operator:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_array.html#Array.sort

This operator should return -1, 0, or 1. According to my very brief
test in irb, an empty serial might be fine while a nil serial will
throw an error.

irb(main):001:0> a = [“serial”, “1”]
=> [“serial”, “1”]
irb(main):002:0> b = ["", “2”]
=> ["", “2”]
irb(main):003:0> a <=> b
=> 1
irb(main):004:0> b = [nil, “2”]
=> [nil, “2”]
irb(main):005:0> a <=> b
=> nil

My guess is that when #sort gets nil instead of -1, 0, or 1, it throws
the error. Of course, I haven’t looked deeply into the source, so I
could be wrong. Again, irb suggests this:

irb(main):006:0> arr = [a, b]
=> [[“serial”, “1”], [nil, “2”]]
irb(main):007:0> arr.sort
ArgumentError: comparison of Array with Array failed
from (irb):7:in `sort’
from (irb):7
from :0

Perhaps you could replace “elt.serial” with “(elt.serial || ‘’)”. If
that doesn’t work - or if my ideas are horrible - somebody else will
help you, I’m sure.

-Kyle

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs