# .find() with simple :join

**URL:** <https://www.ruby-forum.com/t/find-with-simple-join/183315>\
**Category:** Rails\
**Created:** [February 11, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315 "2010-02-11T17:34:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 11, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315/1 "2010-02-11T17:34:29Z")

</div>

Hello all,

I about to pull my hair out.

I have two models “Activities” and “Activity\_types”

## Activities

id  
name  
activity\_type\_id

## Activity\_types

id  
name

- Associations -

class Activity \< ActiveRecord::Base  
has\_many :activity\_types  
end

class ActivityType \< ActiveRecord::Base  
belongs\_to :activity  
end

- activities\_controller -

def index  
@activities = Activity.find(:all, :joins =\> :activity\_types)

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @activities }
end

```

end

- index.html.erb -

# Listing activities
 \<% @activityTypeTitle.each do |title| %\> 

\<% @budgetActivities.each do |activity| %\>

\<% end %\> \<% end %\>

| 
### \<%= title.name %\>
 |
| --- |
| Name | Action |
| --- | --- |
| \<%=h activity.name %\> | \<%= link\_to 'Show', activity %\> | \<%= link\_to 'Edit', edit\_activity\_path(activity) %\> | \<%= link\_to 'Destroy', activity, :confirm =\> 'Are you sure?', :method =\> :delete %\> |
| \<%= link\_to "New Activity", new\_activity\_path %\> |

  

All I’m trying to do is list the ‘types’ as a header of a table with the  
activities in rows.

When I try to do the join, I get…

“SQLite3::SQLException: no such column: activity\_types.activity\_id:  
SELECT “activities”.\* FROM “activities” INNER JOIN “activity\_types” ON  
activity\_types.activity\_id = activities.id”

Thank you for any help with this.

JohnM

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 11, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315/2 "2010-02-11T17:39:00Z")

</div>

You’ve got your has\_many and belongs\_to backwards.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 11, 2010, 5:56pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315/3 "2010-02-11T17:56:58Z")

</div>

Thanks Mat,  
That did it.  
I checked with debug and how do I get the type Name?

John

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 11, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315/4 "2010-02-11T18:10:05Z")

</div>

if I include “:include =\> :activity\_type”, then I get the name.

debug @activities

- !ruby/object:Activity  
activity\_type: &id001 !ruby/object:ActivityType  
attributes:  
name: Budgeting  
created\_at: 2010-01-25 14:38:25  
updated\_at: 2010-01-25 14:38:25  
id: “1”  
attributes\_cache: {}

How to display “name”?

John

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 11, 2010, 7:04pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315/5 "2010-02-11T19:04:59Z")

</div>

Thanks for help.

John

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 11, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315/6 "2010-02-11T18:10:37Z")

</div>

On Thu, Feb 11, 2010 at 11:56, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

You should probably familiarize yourself with this:

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 11, 2010, 8:35pm UTC](https://www.ruby-forum.com/t/find-with-simple-join/183315/7 "2010-02-11T20:35:14Z")

</div>

activity.activitytypes.name
