# Quick Compare Question

**URL:** <https://www.ruby-forum.com/t/quick-compare-question/127427>\
**Category:** Rails\
**Created:** [January 29, 2008, 3:25am UTC](https://www.ruby-forum.com/t/quick-compare-question/127427 "2008-01-29T03:25:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [January 29, 2008, 3:25am UTC](https://www.ruby-forum.com/t/quick-compare-question/127427/1 "2008-01-29T03:25:14Z")

</div>

Hello,  
My controller is supposed to compare values from a particular column in  
two different DB tables to find matches and non-matches. I keep on  
getting an “undefined local variable or method ‘bes’” error. My  
controller looks like this:

class CompareController \< ApplicationController  
def index  
@bes = Bes.find(:all)  
@prov = Import.find(:all)

```
@matches = @prov.select { |prov| prov.prov_service_number ==

```

bes.bes\_phonenumber }  
end  
end

Above, BES is a table housing my company’s BlackBerry mobile phone  
information, and PROV is a table housing our provider’s records. My view  
looks like this:

\<% for import in @matches %\>

 \<%= bes.bes\_displayname %\> \<%= bes.bes\_phonenumber %\> \<%= import.prov\_service\_number %\> \<% end %\>

Can anybody help me with this? I’m still a newbie on the different types  
of variables and RoR in general.

Thank you very much!

- Jeff M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [January 29, 2008, 3:39am UTC](https://www.ruby-forum.com/t/quick-compare-question/127427/2 "2008-01-29T03:39:34Z")

</div>

“@matches = @prov.select { |prov| prov.prov\_service\_number ==  
bes.bes\_phonenumber }”  
should by @matches = @prov.select { |prov| prov.prov\_service\_number ==  
@bes.bes\_phonenumber }

you are declaring a variable but not using it. I dont’ think the view  
will work as is…

On Jan 28, 9:25 pm, Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [January 29, 2008, 3:55am UTC](https://www.ruby-forum.com/t/quick-compare-question/127427/3 "2008-01-29T03:55:24Z")

</div>

On Jan 28, 2008, at 6:25 PM, Jeff M. wrote:

> @bes = Bes.find(:all)  
> looks like this:  
> types  
> of variables and RoR in general.
> 
> Thank you very much!
> 
> - Jeff M.

I think you’ll save yourself some time by doing a find\_by\_sql here:

@matches = Bes.find\_by\_sql(  
“SELECT BES.bes\_phonenumber number, BES.bes\_displayname name,  
PROV.prov\_service\_number prov\_number  
FROM PROV, BES WHERE PROV.provider\_service\_number =  
BES.bes\_phonenumber”)

That way your database can do the heavy lifting, matching up all the  
records. Then in your view:

\<% for match in @matches -%\>

 \<%= match.name %\> \<%= match.number %\> \<%= match.prov\_number %\> \<% end %\>

Does something like this seem sensible?
