# Figuring out the relationships between models

**URL:** <https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173>\
**Category:** Rails\
**Created:** [March 15, 2010, 3:37pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173 "2010-03-15T15:37:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![horsefly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@horsefly](https://www.ruby-forum.com/u/horsefly)\
**Post date:** [March 15, 2010, 3:37pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173/1 "2010-03-15T15:37:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![horsefly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@horsefly](https://www.ruby-forum.com/u/horsefly)\
**Post date:** [March 15, 2010, 3:53pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173/2 "2010-03-15T15:53:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![horsefly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@horsefly](https://www.ruby-forum.com/u/horsefly)\
**Post date:** [March 15, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173/3 "2010-03-15T17:48:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![horsefly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@horsefly](https://www.ruby-forum.com/u/horsefly)\
**Post date:** [March 15, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173/4 "2010-03-15T18:13:51Z")

</div>

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 😉  
And a race doesn’t belong\_to a prediction because there’s no foreign key  
for prediction in the race, is there?

---

<div class="post-metadata">

**Author:** ![horsefly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@horsefly](https://www.ruby-forum.com/u/horsefly)\
**Post date:** [March 15, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173/5 "2010-03-15T21:51:30Z")

</div>

On 15 March 2010 16:48, Mr Horse [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![horsefly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@horsefly](https://www.ruby-forum.com/u/horsefly)\
**Post date:** [March 15, 2010, 10:09pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173/6 "2010-03-15T22:09:01Z")

</div>

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](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 L. wrote:

> On 15 March 2010 16:48, Mr Horse [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![horsefly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@horsefly](https://www.ruby-forum.com/u/horsefly)\
**Post date:** [March 15, 2010, 6:32pm UTC](https://www.ruby-forum.com/t/figuring-out-the-relationships-between-models/185173/7 "2010-03-15T18:32:50Z")

</div>

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 🙂

Aldric G. wrote:

> 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 😉  
> And a race doesn’t belong\_to a prediction because there’s no foreign key  
> for prediction in the race, is there?
