# Join, then view data

**URL:** <https://www.ruby-forum.com/t/join-then-view-data/107036>\
**Category:** Rails\
**Created:** [June 17, 2007, 12:59am UTC](https://www.ruby-forum.com/t/join-then-view-data/107036 "2007-06-17T00:59:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 17, 2007, 12:59am UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/1 "2007-06-17T00:59:04Z")

</div>

I am working on my first real RoR project and have a simple question  
(I think it is anyway).

I have three tables:  
schools  
events  
event\_types

I think I have the joins down with belongs\_to, but how do I actually  
go about displaying that data on the page? I want to display most of  
the info from the events table, but need to pull the school name and  
also the event\_type name. My foreign keys are  
events has a school id and events points to a key in event\_types.

Here is my skeleton display:  
\<% for event in @events %\>  
\<%=h event.event\_name %\>  
\<% end %\>

And my model:

class Event \< ActiveRecord::Base  
belongs\_to :school  
def self.return\_events  
find :all  
end  
end

class School \< ActiveRecord::Base  
has\_many :events  
end

Thank you for saving me from more grey hair!

-Scott

---

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 17, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/2 "2007-06-17T16:45:30Z")

</div>

Hi Scott,

Scott Parks wrote:

> but how do I actually go about displaying that data  
> on the page?

> Here is my skeleton display:  
> \<% for event in @events %\>  
> \<%=h event.event\_name %\>  
> \<% end %\>

Your view gets the data to display from a contoller method of the same  
name.  
Assuming your view file is named skeleton.rhtml, in the skeleton method  
in  
your controller you need to set @events. That would typically result in  
something like…

def skeleton  
@events = Event.find(:all)  
end

Given that you’ve defined a method in your model to do the find (why?),  
you  
could alternatively do

def skeleton  
@events = Event.return\_events  
end

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 17, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/3 "2007-06-17T18:15:43Z")

</div>

Hi Scott,

Scott Parks wrote:

> Thank you for your response,

You’re welcome.

> from both?  
> If I understand what you’re trying to do, you need to use  
> event.school.school\_name. You might want to take a look at the cookbook  
> tutorial at  
> [Radar – O’Reilly](http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html).  
> It sounds like you’re implementing some of the same basic functionality.  
> Take a look at the list.rhtml code on page 2 of Part 2 for the analogy  
> to  
> this particular topic.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 17, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/4 "2007-06-17T17:06:32Z")

</div>

Hi Bill-

Thank you for your response, I have the view working if I try to display  
something from the event table, but what if I want to display something  
from the table that is joined in?

In other words I have  
\<%h event.event\_name %\>  
but I also want to display the school\_name from the schools table…  
\<%h event.school\_name %\>  
will not work because school\_name is in the schools table, so my  
question  
is - I have the tables joined, now how I can display data from both?

Thank you!

-Scott

---

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 18, 2007, 3:28am UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/5 "2007-06-18T03:28:39Z")

</div>

On Jun 17, 2007, at 12:14 PM, Bill W. wrote:

> this particular topic.  
> Bill-

Thank you again, this tutorial has helped me more than the several  
books I have sitting  
at my feet. However, I still can’t get the school\_name to display?

Here is my event\_controller:

class EventController \< ApplicationController  
def index  
list  
render :action =\> ‘list’  
end

def list  
if params[:school\_id].nil?  
@events = Event.find(:all)  
else  
@events = Event.find(:all,  
:conditions =\> [“school\_id = ?”, params[:school\_id]])  
params[:school\_id] = nil  
end  
end

```
 def show
   @event = Event.new
 end

```

end

Then in my list.rhtml I am doing this:

\<% for event in @events %\>  
\<%=h event.event\_name %\>  
\<%=h event.school\_name %\>  
  
  
\<% end %\>

which results in:

undefined method `school\_name’ for #Event:0x259a08c

Any thoughts? Thank you!

-Scott

---

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 18, 2007, 3:44am UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/6 "2007-06-18T03:44:41Z")

</div>

On Jun 17, 2007, at 12:14 PM, Bill W. wrote:

> this particular topic.  
> PSS -  
> I GOT IT! WHEW! Pulling my hair out over this one … here is the  
> deal on what  
> was going on:

I had a table called schools with an id column called school\_id, I  
also have an events  
table with an events\_id column. Events contains a foreign key called  
school\_id. How  
I solved this was to rename the school\_id column to id and the  
events\_id column to  
just id.

WORKS! YAW! Sorry about the confusion, old school thinking here and  
was missing this  
step!

Thank you again for your help! I am sure I will have more questions  
soon enough.

-Scott

---

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 18, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/7 "2007-06-18T15:05:04Z")

</div>

Hi Scott,

Scott Parks wrote:

> here and was missing this step!  
> Glad to hear you got it figured out!

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![Scott\_Parks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Scott\_Parks](https://www.ruby-forum.com/u/Scott_Parks)\
**Post date:** [June 18, 2007, 3:32am UTC](https://www.ruby-forum.com/t/join-then-view-data/107036/8 "2007-06-18T03:32:40Z")

</div>

On Jun 17, 2007, at 12:14 PM, Bill W. wrote:

> this particular topic.  
> PS:

I did try

\<% for event in @events %\>  
\<%=h event.event\_name %\>  
\<%=h event.school.school\_name %\>  
  
  
\<% end %\>

Which gives me:

Mysql::Error: #42S22Unknown column ‘schools.id’ in ‘where clause’:  
SELECT \* FROM schools WHERE (schools.`id` = 1)

The table name is schools with school\_id, NOT schools\_id, where is it  
coming up with an s on the end of the id?

-Scott
