# Need help configuring this data model

**URL:** <https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128>\
**Category:** Rails\
**Created:** [March 28, 2009, 9:24am UTC](https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128 "2009-03-28T09:24:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 28, 2009, 9:24am UTC](https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128/1 "2009-03-28T09:24:10Z")

</div>

Consider 2 tables:  
Player-\> column player\_id  
and  
Hit-\> columns player\_id, hit\_player\_id

I want one model for Player and one model for a Hit.  
Any player has many hits against another player.  
So from a Player I want to be able to get all the Hits, and from the  
Hit get the hit Player.  
On the other hand, from a Player I want to retrieve all Hits against  
that player (were player\_id = hits.hit\_player\_id)  
and the player\_id from that hit.

So far I have:

class Player \< ActiveRecord::Base  
has\_many :hits  
end

and

class Hit \< ActiveRecord::Base  
belongs\_to :player  
end

But I can’t figure out how to map the ‘hit’ player.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 28, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128/2 "2009-03-28T17:14:58Z")

</div>

A quick note first - Player most likely shouldn’t have a player\_id  
column - the Rails convention is to simply call it id.

What you’re looking for are some of the options to has\_many:

class Player \< ActiveRecord::Base  
has\_many :outgoing\_hits, :class\_name =\> ‘Hit’, :foreign\_key =\>  
‘hitter\_id’  
has\_many :incoming\_hits, :class\_name =\> ‘Hit’, :foreign\_key =\>  
‘hittee\_id’

has\_many :hittees, :through =\> :outgoing\_hits  
has\_many :hitters, :through =\> :incoming\_hits  
end

class Hit \< ActiveRecord::Base  
belongs\_to :hitter, :class\_name =\> ‘Player’  
belongs\_to :hittee, :class\_name =\> ‘Player’  
end

Given a Player object, this is what you can do:

player.outgoing\_hits # =\> list of hits by the player  
player.incoming\_hits # =\> list of hits to the player  
player.hittees # =\> list of all players hit by this player  
player.hitters # =\> list of all players hitting this player

You can switch the names around, but that’s the idea.

–Matt J.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 28, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128/3 "2009-03-28T18:07:46Z")

</div>

Thanks very much, I will give that a try. Yes I did have the column  
for player\_id as “id”, I mis-typed that, but its a good reminder,  
appreciated.

-Brad

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 29, 2009, 1:55am UTC](https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128/4 "2009-03-29T01:55:07Z")

</div>

This approach doesn’t seem to be working for me.

I’ve set up the models as described above.  
class Player \< ActiveRecord::Base  
has\_many :outgoing\_hits, :class\_name =\> ‘Hit’, :foreign\_key =\>  
‘player\_id’  
has\_many :incoming\_hits, :class\_name =\> ‘Hit’, :foreign\_key =\>  
‘hit\_player\_id’

has\_many :hittees, :through =\> :outgoing\_hits  
has\_many :hitters, :through =\> :incoming\_hits  
end

class Tag \< ActiveRecord::Base  
belongs\_to :hitter, :class\_name =\> ‘Player’  
belongs\_to :hittee, :class\_name =\> ‘Player’  
end

For player foo, I’m trying to list all the name of all players hit.

\<% @player.outgoing\_hits.each do |hit| %\>- \<%=hit.hittee.screen\_name%\>
\<%end%\>

But, “hittee” is null, although I can access other properties of the  
hit.

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.screen\_name

Here’s the key columns of the 2 tables involved:  
Player{  
id:integer  
screen\_name:varchar  
}

Hit{  
id:integer  
player\_id:integer  
hit\_player\_id:integer  
}

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 29, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128/5 "2009-03-29T23:14:38Z")

</div>

Correcting the column names did the trick. Is there a similar  
mechanism to the foreign-key hnit that can be used? I thought there  
used to be an identified-by keyword?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 29, 2009, 11:09am UTC](https://www.ruby-forum.com/t/need-help-configuring-this-data-model/164128/6 "2009-03-29T11:09:14Z")

</div>

Your associations and table columns don’t match. You have to tell  
Rails how to map hitter and hittee if you are not following  
convention. By default foreign key for a belongs\_to association is the  
name of the association with an “\_id” suffix.

So if you want to follow convention, you change Hit-table to be like  
this:

Hit {  
id:integer  
hitter\_id:integer  
hittee\_id:integer  
}

and change Player-class to use those column names (like in Matt J.’  
example).
