Forum: Ruby on Rails Questions about ActiveRecord

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Folke Will (Guest)
on 2006-02-18 21:25

I have a few questions about data base design and integrating the design
into Rails.

I'm creating a CMS for a local sports club and want to store the clubs,
their teams and the matches in a data base.

Here is the schema I made up:

As you can see, I added home_id and guest_id to each match that link to
the competing teams. My problem is telling Rails about this kind of
relation. I want to be able to do something like this:

# This should return all matches where is home_id or guest_id:
<% for match in @team.matches %>
  <%= match.home_points %>
<% end %>

Adding has_many(:matches) in team.rb and has_many(:teams) in match.rb
didn't work because there is no join table.

That's why I added another solution to the schema using a join table
(bottom of the pasted file). But that led me to another problem, I don't
know how to get the team's points using that solution. And I don't like
that solution because I'd have to check that each match has exactly two
entries in the join table, etc.

Any suggestions?

Peter Gerlmann (Guest)
on 2006-02-19 16:44
did you try using :foreign_key in belongs_to?
This topic is locked and can not be replied to.