Questions about ActiveRecord



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?



did you try using :foreign_key in belongs_to?