Figuring out the relationships between models.

Hi Guys,
I'm trying to come up with a model layout for a simple enough race
prediction app but am a little stuck. Basically, the app will allow
users to enter their predictions for a number of races (20 races in
total). When the races have been finished, I want to determine who
predicted all of the race winners correctly, who got 19/20, who got
18/20 etc.

Here's a summary of the players and their relationships:

A User has one Betslip.
A Betslip has many Predictions (one Prediction for each of the 20
races).
Each Prediction consists of one Race and one Racer (the predicted-to-win
Racer).
Each Race has a number of Racers.
Each Race has one Winner (also a Racer).

This is where I'm getting a little confused.
Each Prediction consists of one Race and one Racer, but a Race also
consists of a number of Racers. A Race will also have one Winner (also a
Racer). It seems like there are three types of Racers - a general racer
(linked to a race), a predicted-to-win racer (linked to a prediction)
and a winning racer (linked to a race).

Would anyone have any advice on the best way to model this?
At the end of all of the races, the main thing I want to do is compare
each user's betslip against the actual winners to see how many of their
predictions were correct.

Many thanks,
Eddie

Mr Horse wrote:

Hi Guys,
I'm trying to come up with a model layout for a simple enough race
prediction app but am a little stuck. Basically, the app will allow
users to enter their predictions for a number of races (20 races in
total). When the races have been finished, I want to determine who
predicted all of the race winners correctly, who got 19/20, who got
18/20 etc.

Here's a summary of the players and their relationships:

A User has one Betslip.
A Betslip has many Predictions (one Prediction for each of the 20
races).
Each Prediction consists of one Race and one Racer (the predicted-to-win
Racer).
Each Race has a number of Racers.
Each Race has one Winner (also a Racer).

User has_one :betslip
Betslip belongs_to :user
Betslip has_many :predictions
Prediction belongs_to :betslip
Prediction has_one :race
Prediction has_one :racer
Race has_many :racers
Race has_one :winner, :through => :racers

Racers .... Are separate? Unless each racer can only participate in one
race, in which case Racer belongs_to :race

I think that's basically what you're looking for.

Thanks for the advice Aldric. This approach makes sense, but I get a
little confused with the relationship between predictions and races.

A prediction has_one race, but does this mean that a race must belong_to
a prediction? While the first statement is correct (a prediction has_one
race), a race has many predictions associated with it. Is it possible to
state that a "race has_many predictions" as well as saying that a
"prediction has_one race"?

I could say that a race has_many predictions and a prediction belongs_to
a race, but it makes more sense to me to think of a prediction having a
race as opposed to the other way around (a prediction belonging to a
race).

Eddie

Mr Horse wrote:

Thanks for the advice Aldric. This approach makes sense, but I get a
little confused with the relationship between predictions and races.

A prediction has_one race, but does this mean that a race must belong_to
a prediction? While the first statement is correct (a prediction has_one
race), a race has many predictions associated with it. Is it possible to
state that a "race has_many predictions" as well as saying that a
"prediction has_one race"?

I could say that a race has_many predictions and a prediction belongs_to
a race, but it makes more sense to me to think of a prediction having a
race as opposed to the other way around (a prediction belonging to a
race).

Eddie

A horse is a horse, of course of course... Oh, sorry. I was distracted.

A prediction definitely doesn't belong to a race :wink:
And a race doesn't belong_to a prediction because there's no foreign key
for prediction in the race, is there?

I guess my question is this: if you declare that 'a has_one b', is it a
requirement that you must also state 'b belongs_to a' (i.e. can you have
a 'has_one' without a corresponding 'belongs_to' ?). If this requirement
holds true and I state that a prediction has_one race, then I must also
state that a race belongs_to a prediction. I may be mistaken here, but
this wouldn't allow me to retrieve all of the predictions associated
with a given race (and a race doesn't really belong to a prediction).

Mr. Ed :slight_smile:

Aldric Giacomoni wrote:

Thanks for the advice Aldric. This approach makes sense, but I get a
little confused with the relationship between predictions and races.

A prediction has_one race, but does this mean that a race must belong_to
a prediction? While the first statement is correct (a prediction has_one
race), a race has many predictions associated with it. Is it possible to
state that a "race has_many predictions" as well as saying that a
"prediction has_one race"?

No, as neither model would have the foreign key column, see below.

I could say that a race has_many predictions and a prediction belongs_to
a race, but it makes more sense to me to think of a prediction having a
race as opposed to the other way around (a prediction belonging to a
race).

This is the right way to do it even though prediction belongs_to race
sounds a bit odd. The model that 'belongs_to' is the one with the the
foreign key (prediction has a column race_id in this case). You will
find that belongs_to often does not seem quite the correct way of
stating the relationship, but if the has_many seems right (race
has_many predictions) and each of the predictions (in this case) is
associated with one (race in this case) then that is the way to do it.

Colin

Thanks Colin,
I understand it a bit better now (this article also helped:
http://duanesbrain.blogspot.com/2006/05/ruby-on-rails-hasone-versus-belongsto.html).
The terms used in Rails to describe associations can be pretty
confusing.
Eddie

Colin Law wrote: