Use data from multiple tables in one view


#1

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


#2

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


#3

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.


#4

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


#5

class Note < ActiveRecord::Base
has_many :type
end

class Type < ActiveRecord::Base
belongs_to :note
end


#6

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


#7

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.