# Using Variables after Find

**URL:** <https://www.ruby-forum.com/t/using-variables-after-find/126860>\
**Category:** Rails\
**Created:** [January 22, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/using-variables-after-find/126860 "2008-01-22T22:05:52Z")\
**Posts on this page:** 6\
**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 22, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/using-variables-after-find/126860/1 "2008-01-22T22:05:52Z")

</div>

Hello,  
I am still new to RoR and I’m having a very basic problem… Ok, so I  
can display all the results of two different tables by calling (in my  
case)

@bes = Bes.find(:all)  
@prov = Import.find(:all)

This works great, however, I need to take information from both of these  
and put them in a new variable to display (multi-row). How am I to do  
this? I tried to use stuff like @newrow = Bes.bes\_phonenumber , etc…  
but I am not having any luck. Can anybody show me how to do this  
properly?

Thank you all 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 22, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/using-variables-after-find/126860/2 "2008-01-22T22:28:10Z")

</div>

@bes and @prov should now contain all the data you need for your  
views.

In your views, you would then get the data out with:

\<% @bes.each do |x| %\>  
\<%= x.the\_name\_of\_one\_of\_your\_tables\_here %\>  
\<% end %\>

or

\<% @prov.each do |x| %\>  
\<%= x.the\_name\_of\_one\_of\_your\_tables\_here %\>  
\<% end %\>

try that

jackster.mobi

On Jan 22, 4:05 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 22, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/using-variables-after-find/126860/3 "2008-01-22T22:30:28Z")

</div>

Jeff M. wrote:

> Hello,  
> I am still new to RoR and I’m having a very basic problem… Ok, so I  
> can display all the results of two different tables by calling (in my  
> case)
> 
> @bes = Bes.find(:all)  
> @prov = Import.find(:all)
> 
> This works great, however, I need to take information from both of these  
> and put them in a new variable to display (multi-row). How am I to do  
> this? I tried to use stuff like @newrow = Bes.bes\_phonenumber , etc…  
> but I am not having any luck. Can anybody show me how to do this  
> properly?
> 
> Thank you all very much,
> 
> - Jeff M.

erm…as “find” returns an array you have to access each element in that  
array one by one…so you’d do @bes[0].bes\_phone\_number then  
@bes[1].bes\_phone\_number…

or:

@bes.each do |i|  
@bes[i].bes\_phonenumber  
end

though i’m a noob too so there’s probably a better way of doing this…?

do you know about the ruby console where you can test out bits of code  
like this? run ruby script/console and you can play around with  
variable assignments and what not

---

<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 22, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/using-variables-after-find/126860/4 "2008-01-22T23:22:50Z")

</div>

Jeff M. wrote:

> sweet, thanks guys! I have another question, though… If all of this is  
> being done in the view, then if I have to make changes to some of the  
> variables, I have to that in the view too, right? I used this line to  
> change the string length in the view:
> 
> \<%= bes.bes\_phonenumber if bes.bes\_phonenumber.size==11  
> bes.bes\_phonenumber=string[1…11] %\>
> 
> It is supposed to cut off the first digit of the phone number if the  
> length of the number is 11 characters long. However, this doesn’t work  
> (compile error). Can somebody clarify this for me?
> 
> I appreciate everyone’s help, thank you!
> 
> - Jeff M.  
> you mean you want to say: if the phonenumber’s size is 11 cut the first  
> digit from it right?

i’d say:

\<%if bes.bes\_phonenumber.to\_s.size == 11%\>  
\<%bes.bes\_phonenumber = bes.bes\_phonenumber.to\_s.slice(1…11)%\>  
\<%bes.bes\_phonenumber.to\_i%\>

so here in line 1 you get the string size of bes\_phonenumber then in  
line 2take off the leading digit and store the result in  
bes\_phonenumber…but then bes\_phonenumber is a string so the last line  
converts it back to an int

this is without doubt NOT the best way of doing this (in fact it’s  
probably the worst), but it works…

---

<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 22, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/using-variables-after-find/126860/5 "2008-01-22T23:03:55Z")

</div>

sweet, thanks guys! I have another question, though… If all of this is  
being done in the view, then if I have to make changes to some of the  
variables, I have to that in the view too, right? I used this line to  
change the string length in the view:

\<%= bes.bes\_phonenumber if bes.bes\_phonenumber.size==11  
bes.bes\_phonenumber=string[1…11] %\>

It is supposed to cut off the first digit of the phone number if the  
length of the number is 11 characters long. However, this doesn’t work  
(compile error). Can somebody clarify this for me?

I appreciate everyone’s help, thank you!

- 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 22, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/using-variables-after-find/126860/6 "2008-01-22T23:27:58Z")

</div>

That works! Thank you for you help! You guys here at Ruby-Forum know  
what you’re talking about!
