Forum: Ruby on Rails Use data from multiple tables in one view.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
57d4b5dd0354186b154b376917c43c24?d=identicon&s=25 Matt Monsen (mdmonsen)
on 2009-02-22 12:58
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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2009-02-22 13:51
(Received via mailing list)
On Sun, Feb 22, 2009 at 3:58 AM, Matt Monsen <
rails-mailing-list@andreas-s.net> 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
57d4b5dd0354186b154b376917c43c24?d=identicon&s=25 Matt Monsen (mdmonsen)
on 2009-02-22 13:59
>
> 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.
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2009-02-22 14:15
(Received via mailing list)
On Sun, Feb 22, 2009 at 4:59 AM, Matt Monsen <
rails-mailing-list@andreas-s.net> 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
57d4b5dd0354186b154b376917c43c24?d=identicon&s=25 Matt Monsen (mdmonsen)
on 2009-02-22 14:25
class Note < ActiveRecord::Base
  has_many :type
end

class Type < ActiveRecord::Base
  belongs_to :note
end
992f6dd317a9b164e1efc5b674623f38?d=identicon&s=25 Michael Graff (Guest)
on 2009-02-23 00:46
(Received via mailing list)
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
57d4b5dd0354186b154b376917c43c24?d=identicon&s=25 Matt Monsen (mdmonsen)
on 2009-02-23 08:46
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.
This topic is locked and can not be replied to.