# Use data from multiple tables in one view

**URL:** <https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021>\
**Category:** Rails\
**Created:** [February 22, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021 "2009-02-22T12:58:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [February 22, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021/1 "2009-02-22T12:58:33Z")

</div>

Thanks for help in advance with this.

I have been trying to understand how to get this to work correctly and I  
have been having trouble getting it to work the way that I think it  
should.

I would like to have something like the following:

Table: Note  
id  
text  
type\_id

Table: Type  
id  
type\_name

I have been able to create the data and put it into the database without  
any problem. I cannot seem to reference the type\_name when I am trying  
to do a show for my notes.

I have tried to do a few different things and I see there could be a  
couple ways to do this so I would like to know if anyone has an example  
of how they would do this.

One method I tried was in my view was something like the following:

\<% for note in @notes %\>  
\<%= h(@types.find(type\_id).type\_name) %\>  
\<% end %\>

And the result of this gives me an error:

LocalJumpError in Employees#show  
no block given

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [February 22, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021/2 "2009-02-22T13:51:56Z")

</div>

On Sun, Feb 22, 2009 at 3:58 AM, Matt Monsen \<  
removed\_email\_address@domain.invalid\> wrote:

> id
> 
> And the result of this gives me an error:
> 
> LocalJumpError in Employees#show  
> no block given

Hey Matt, what’s the relationship or association between a ‘note’ and a  
‘type’?

-Conrad

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [February 22, 2009, 1:59pm UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021/3 "2009-02-22T13:59:33Z")

</div>

> Hey Matt, what’s the relationship or association between a ‘note’ and a  
> ‘type’?

Right now I have it set up as a one-to-many relationship. I am not sure  
that that is the correct relationship that I want for this however.  
Basically the type will be a |Coaching|Attendance|Performance| note and  
the idea is that there will be more types added in later.

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [February 22, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021/4 "2009-02-22T14:15:27Z")

</div>

On Sun, Feb 22, 2009 at 4:59 AM, Matt Monsen \<  
removed\_email\_address@domain.invalid\> wrote:

> > Hey Matt, what’s the relationship or association between a ‘note’ and a  
> > ‘type’?
> 
> Right now I have it set up as a one-to-many relationship. I am not sure  
> that that is the correct relationship that I want for this however.  
> Basically the type will be a |Coaching|Attendance|Performance| note and  
> the idea is that there will be more types added in later.

What does the model files look like?

-Conrad

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [February 22, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021/5 "2009-02-22T14:25:34Z")

</div>

class Note \< ActiveRecord::Base  
has\_many :type  
end

class Type \< ActiveRecord::Base  
belongs\_to :note  
end

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [February 23, 2009, 12:46am UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021/6 "2009-02-23T00:46:49Z")

</div>

You’re making it harder than it needs to be, and I don’t think you’re  
understanding the relationships you are setting up.

Since a Note has many types, if you did this at the console:

> > > note = Note.find :first

you would have a note. To list its types:

> > > note.types

Since this is an array, you would have to find all the types this note  
had if you wanted to, say, display them. Something like:

note.types.each do |type|  
puts type.type\_name  
end

(in standard non-erb lingo)

If you were wanting to print all the types as you were trying to do in a  
view:

\<% @notes.each do |note| %\>  
\<% note.types.each do |type| %\>  
\<%= h(type.type\_name) %\>  
\<% end %\>  
\<% end %\>

Or, more simply:

\<% @notes.each do |note| %\>  
\<%= h(note.types.join(", ") %\>  
\<% end %\>

I would consider getting rid of the name “type\_name” and instead  
calling it “name”. Also, you might want “has\_many :types” rather than  
“has\_many :type”

On Sun, Feb 22, 2009 at 7:25 AM, Matt Monsen

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [February 23, 2009, 8:46am UTC](https://www.ruby-forum.com/t/use-data-from-multiple-tables-in-one-view/161021/7 "2009-02-23T08:46:06Z")

</div>

Thank you for your help with this. I knew I was looking into this and  
making it harder than it needed to be. It ended up that when I started  
second guessing myself I was troubleshooting the wrong issue.

I was getting nil values on some of my note types which was causing the  
error and this was because I put the column in after I had initially set  
up the database. Previous notes didn’t have a type and thats where my  
problem was. I was looking at it that I had my relationship set up  
incorrectly and that I wasn’t referencing it properly.
