Search code examples
ruby-on-railsrubyruby-on-rails-2

Ruby Ternary If Bug


What I'm trying to do is print Pending Quotes if the count is 0 or >1 and Pending Quote if the count ==1 but if the count is >1, the output is 2 true, the other two cases work fine though and I can't see anything obvious.

<%= @pending.nil? ? '0' : @pending.count %>
<%= (!@pending.nil? and @pending.count > 1) or (!@pending.nil? and @pending.count == 0) ? 'Pending Quotes' : 'Pending Quote' %>

Solution

  • You have to write this way :

    ((!@pending.nil? and @pending.count > 1) or (!@pending.nil? and @pending.count == 0)) ? 'Pending Quotes' : 'Pending Quote'
    

    You could also write it as

    (!@pending.nil? && (@pending.count > 1 || @pending.count == 0)) ? 'Pending Quotes' : 'Pending Quote'