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 Jones

On Mar 27, 9:06 pm, Brad A <[email protected]> wrote:
> 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.
>

--~--~---------~--~----~------------~-------~--~----~
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