# Finding Associated Records

**URL:** <https://www.ruby-forum.com/t/finding-associated-records/124465>\
**Category:** Rails\
**Created:** [December 28, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/finding-associated-records/124465 "2007-12-28T23:29:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [December 28, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/finding-associated-records/124465/1 "2007-12-28T23:29:11Z")

</div>

I am trying to find all associated tickets with an employee. I am very  
new to HABTM so I am having some trouble.

view: time.html.erb

```auto
<h1>Time Report for <%= @employee.name %></h1>

<table cellpading="10" cellspacing="10" style="width:700px">
  <thead>
    <tr>
      <th>Job #</th>
      <th>Company</th>
      <th>Project</th>
      <th></td>
    </tr>
  </thead>
  <tbody>
  <% for ticket in @employee %>
    <tr class="<%= cycle('odd', 'even') %>">
      <td>
        <%= @employee.ticket.id %>
      </td>
    </tr>
  <% end %>
</table>
```

Controller:

```auto
  def time
    @employee = Employee.find(params[:id], :include => { :tickets =>
:workorder} )
  end
```

Employee Model:

```auto
class Employee < ActiveRecord::Base
  has_and_belongs_to_many :tickets

  validates_uniqueness_of :name
end
```

Ticket Model

```auto
class Ticket < ActiveRecord::Base
  has_and_belongs_to_many :employees
  belongs_to :workorder
end
```

Error

```auto
 NoMethodError in Employees#time

Showing employees/time.html.erb where line #13 raised:

undefined method `each' for #<Employee:0x326b044>
```

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [December 29, 2007, 12:18am UTC](https://www.ruby-forum.com/t/finding-associated-records/124465/2 "2007-12-29T00:18:16Z")

</div>

One problem is here:

\<% for ticket in @employee %\>  
…  
\<% end %\>

You need:

\<% for ticket in @employee.tickets %\>  
…  
\<% end %\>

OR

\<% @employee.tickets.each do |ticket| %\>  
…  
\<% end %\>

Secondly, make sure you have a join table when doing a habtm  
relationship.
