# Showing data from a join table ( HABTM )

**URL:** <https://www.ruby-forum.com/t/showing-data-from-a-join-table-habtm/145992>\
**Category:** Rails\
**Created:** [August 15, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/showing-data-from-a-join-table-habtm/145992 "2008-08-15T22:09:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 15, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/showing-data-from-a-join-table-habtm/145992/1 "2008-08-15T22:09:05Z")

</div>

I have two tables with a simple has\_and\_belongs\_to\_many relationship.

class Author \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :sources  
end

class Source \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :authors  
end

Naturally I have an Authors table, a Sources table, and an  
AuthorsSources table.

I have created a form with no problem, and the form saves the authors  
and sources and their relationship correctly.

Now I’m trying to set up the view for the show method, where users can  
see the Source and all of the Authors associated with it.

How do I go about doing this? I’ve looked all over the place, and can’t  
find any pointers. Let me know if you need any more information.

Thank you!

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 15, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/showing-data-from-a-join-table-habtm/145992/2 "2008-08-15T22:34:22Z")

</div>

Morgan K. wrote:

> I have two tables with a simple has\_and\_belongs\_to\_many relationship.
> 
> class Author \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :sources  
> end
> 
> class Source \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :authors  
> end
> 
> Naturally I have an Authors table, a Sources table, and an  
> AuthorsSources table.
> 
> I have created a form with no problem, and the form saves the authors  
> and sources and their relationship correctly.
> 
> Now I’m trying to set up the view for the show method, where users can  
> see the Source and all of the Authors associated with it.
> 
> How do I go about doing this? I’ve looked all over the place, and can’t  
> find any pointers. Let me know if you need any more information.
> 
> Thank you!

Well, I guess you’d want to start in your SourcesController#show method.

def show  
@source = Source.find(params[:id])  
end

Then in your view, something like this:  
\<% @source.authors.each do |author| %\>  
\<%= author.full\_name %\>  
\<%end%\>

You should certainly play around in script/console to get a feel for the  
instance methods that come along with a habtm relationship.  
See “Auto Generated Methods” found here:  
[http://www.ruby-forum.com/topic/162890?reply\_to=715361#postform](http://www.ruby-forum.com/topic/162890?reply_to=715361#postform)

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 15, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/showing-data-from-a-join-table-habtm/145992/3 "2008-08-15T22:34:56Z")

</div>

> [http://www.ruby-forum.com/topic/162890?reply\_to=715361#postform](http://www.ruby-forum.com/topic/162890?reply_to=715361#postform)

Replace that link with this:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

Lake

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 15, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/showing-data-from-a-join-table-habtm/145992/4 "2008-08-15T22:42:42Z")

</div>

> Then in your view, something like this:  
> \<% @source.authors.each do |author| %\>  
> \<%= author.full\_name %\>  
> \<%end%\>

Thank you! I was trying to make it way too hard on myself by trying to  
access the join table… Rails is often more automagic than I expect it  
to be!
