I want to sort my records in an rails application:
@ebms = Ebm.all
@ebms.sort_by! {|u| u.number}
The u.number
is defined as integer!
The problem is that Rails cannot compare it with nil
:
comparison of NilClass with 32400 failed
What can i do to evade this error?
How about to try convert nil to integer?
@ebms = Ebm.all
@ebms.sort_by! { |u| u.number.to_i }
You can add a default value for the comparison that will be used when number
is nil:
@ebms = Ebm.all
@ebms.sort_by! {|u| u.number || 0}
Or you can follow the suggestions in this answer to select those with a number and sort them, then add those without a number to the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With