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
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
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
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
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
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