Wasi Khn
Wasi Khn

Reputation: 55

I want to show data from two tables in a single view

I have two tables: one named um_org_data and the other addresses.

The problem is that I want to show the data from um_org_data with addresses where addresses has a foreign key um_org_datum_id.

Here is the code of my view in which I want to show the data from two tables together:

<p id="notice"><%= notice %></p>


<div class="container">
  <div class="row">
      <div class="span3 pull-right">
        <div class="well">
          <h2>Heading</h2>
          <p>Sample text</p>
        </div> 
      </div> 

    <div class="span9">
    <h2>Organization Details</h2>

    <table class="table table-hover">
      <tr>
        <th col span="1" style="width: 200px">
            </i>&nbsp;&nbsp;&nbsp;Organization Name:
        </th>
        <td><%= @um_org_datum.org_name %></td>
      </tr>
      <tr>
        <th col span="1" style="width: 250px">
            </i>&nbsp;&nbsp;&nbsp;Organization Description:
        </th>
        <td><%= @um_org_datum.org_description %></td>
      </tr>
      <tr>
        <th col span="1" style="width: 250px">
          </i>&nbsp;&nbsp;&nbsp;Web Domain:
        </th>
        <td><%= @um_org_datum.webdomain %></td>
      </tr>
      <tr>
        <th col span="1" style="width: 200px">
          <%= fields_for :address_attributes do |p| %>
            <%= p.label 'Office Address' %><br />              
        </th>
        <td><%= p.address.offc_addr %></td>
        <% end %>
      </tr>
      <tr>
        <th col span="1" style="width: 200px">
            </i>&nbsp;&nbsp;Office Phone Number:
        </th>
        <td><%= @um_org_datum.offc_ph %></td>
      </tr>
    </table>

    <div class="control-group">
      <div class="controls">       
       <a class="btn" href="/um_org_data" style="text-color:black">View all</a>


       </div>
    </div>
</div>
</div>
</div>

I don't use any join query in the controller as I don't know how to use join query. If this problem needs a join query, please tell me what is the syntax and what I have to change in the view form to show attributes of both tables.

table name: um_org_data , attributes: id, oeg_name, org_description, webdomain

table name: adrresses , attributes: id, offc_addr, um_org_datum_id

Thanks in advance!

Upvotes: 0

Views: 881

Answers (2)

Matthias
Matthias

Reputation: 4375

Do you have 1:n association? Am I right?

Definitely remove the fields_for...you are not in a form!

Change this:

  <tr>
    <th col span="1" style="width: 200px">
      <%= fields_for :address_attributes do |p| %>
        <%= p.label 'Office Address' %><br />              
    </th>
    <td><%= p.address.offc_addr %></td>
    <% end %>
  </tr>

to this:

  <tr>
    <th col span="1" style="width: 200px">Office Address</th>
    <%= @um_org_datum.addresses.each do |p| %>
     <td><%= p.office_addr %></td>
    <% end %>
  </tr>

Check out the markup, but the logic should be clear now..

Upvotes: 1

StickMaNX
StickMaNX

Reputation: 1982

I'm assuming you have the following in your models

um_org_datum

has_many :addresses

address

belongs_to :um_org_datum

Does this work?

  <tr>
    <th col span="1" style="width: 200px">Office Address</th>
    <%= @um_org_datum.addresses.each do |p| %>
     <td><%= p.offc_addr %></td>
    <% end %>
  </tr>

Upvotes: 0

Related Questions