Topic: Help w/ pulling data from 2 tables and displaying the results

Hello,

I'm brand new to ruby and I'm having trouble pulling data from one
table (table_1), looping through the results and pulling data from
another table (table_2) and displaying the results of table_2 in one
field.

the pseudo code would be as follows:

result_1 = select first_name from table_1
loop results_1
{
      puts "<tr><td> results_1.first_name<td>"

      results_2 = select table_2.info from table_2 where table_1.user_id = table_2.user_id;
        puts "<td>"
        loop results_2
        {
             puts "<li>table_2.info
        }
        puts "</td>"
}


The display would look like this for 1 row:

joe | -info 1
-info 2
-info 3

Help - I'm desparate!
clem_c_rock is online now Report Post Edit/Delete Message

Re: Help w/ pulling data from 2 tables and displaying the results

I recommend going through a Ruby on Rails tutorial/book. Normally you reference tables through models, so let's say you have the two models Category and Product (with categories and products tables). The relationship is Category has_many :products. You can display each category along with each product in the category like this:

# in controller
@categories = Category.find(:all)

# in view
<% for category in @categories %>
  <%= category.name %>
  <% for product in category.products %>
    <%= product.name %>
  <% end %>
<% end %>

Railscasts - Free Ruby on Rails Screencasts

Re: Help w/ pulling data from 2 tables and displaying the results

You should not double post.

Re: Help w/ pulling data from 2 tables and displaying the results

From now on let's keep it to the other thread.

Josh Catone helps run this place
Rails Forum - Rails Jobs