Using Variables after Find

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 Miller

@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

Jeff Miller 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 Miller

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

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 Miller

Jeff Miller 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 Miller

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....

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