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:
> On 15 March 2010 16:48, Mr Horse <[email protected]> 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

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to