Comparison not working...

Hello,
I am getting a very strange error... I am working on a view that
compares a phone number in one model to a phone number in another model
and displays the matches and non-matches. However, when I try to do a
comparison using != it just pumps out hundreds of repeating lines. My
view looks like this:

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="50%" valign="top">
  <table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr><td colspan="2">
      <font size="2" face="verdana"><b>Matches</b></font>
  </td></tr>
  <% for bes in @bes %>
  <% if bes.bes_phonenumber.to_s.size == 11 %>
  <% bes.bes_phonenumber = bes.bes_phonenumber.to_s.slice(1..11) %>
  <% bes.bes_phonenumber.to_i %>
  <% end %>

  <% for import in @prov %>
  <% if bes.bes_phonenumber == import.prov_service_number %>

  <tr>
    <td><%= bes.bes_displayname %></td>
    <td><%= bes.bes_phonenumber %></td>
  </tr>
  <% end %>

  <% end %>
  <% end %>
  </table>
</td>
<td width="50%" valign="top">
  <table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr><td colspan="2"><font size="2"
face="verdana"><b>Non-Matches</b></font></td></tr>
    <% for bes in @bes %>
  <% if bes.bes_phonenumber.to_s.size == 11 %>
  <% bes.bes_phonenumber = bes.bes_phonenumber.to_s.slice(1..11) %>
  <% bes.bes_phonenumber.to_i %>
  <% end %>

  <% for import in @prov %>
  <% if bes.bes_phonenumber == import.prov_service_number returns false
%>

  <tr>
    <td><%= bes.bes_displayname %></td>
    <td><%= bes.bes_phonenumber %></td>
  </tr>
  <% end %>

  <% end %>
  <% end %>
  </table>
</td>
</tr>
</table>

The first one works perfectly (I think) but the second just pumps out
about 100 rows of each bes_displayname and bes_phonenumber. Does anybody
know what is causing the error? I tried to find a comparison operator in
Ruby to say "not equals" but came up short... Is != not part of Ruby?

Thanks,
- Jeff Miller

oops, what I posted was the wrong thing, sorry...

The relevant part of the code is this:

    <% for bes in @bes %>
  <% if bes.bes_phonenumber.to_s.size == 11 %>
  <% bes.bes_phonenumber = bes.bes_phonenumber.to_s.slice(1..11) %>
  <% bes.bes_phonenumber.to_i %>
  <% end %>

  <% for import in @prov %>
  <% if bes.bes_phonenumber != import.prov_service_number %>

  <tr>
    <td><%= bes.bes_displayname %></td>
    <td><%= bes.bes_phonenumber %></td>
    <td><%= import.prov_service_number %></td>
  </tr>
  <% end %>

  <% end %>
  <% end %>

Thanks!

What is bes and prov? Please don’t shorten the variable names as it makes it harder for other programmers to understand what the code is about.

You can do a select method call on the @prov collection and put the condition in there, without the if.

@prov.select { |prov| prov.number != bes.number }

<td width="50%" valign="top">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td colspan="2"><font size="2"
face="verdana"><b>Non-Matches</b></font></td></tr>
   <% for bes in @bes %>
<% if bes.bes_phonenumber.to_s.size == 11 %>
<% bes.bes_phonenumber = bes.bes_phonenumber.to_s.slice(1..11) %>
<% bes.bes_phonenumber.to_i %>
<% end %>

Random guess: you think bes.bes_phonenumber.to_i is actually changing
bes.bes_phonenumber. It isn't

<% for import in @prov %>
<% if bes.bes_phonenumber == import.prov_service_number returns false
%>

Not sure what you're on about here, 'returns' isn't a ruby thing, and
rather meaningless in the context of a view.
All this messing around with slicing strings etc. either belongs in a
model (if this is a data normalization thing) or in a helper of some
sort, as it makes views confusing and/or messy

Fred