I have the following in a model class:

class Game < ActiveRecord::Base
        has_many :player_stats
        has_many :players, :through => :player_stats

        def visitor_stats
                stats = []
                player_stats.each do |stat|
                        stats << stat   if stat.player.team.team_code ==
self.visiting_team_code
                end
                stats
        end

        def home_stats
                stats = []
                player_stats.each do |stat|
                        stats << stat   if stat.player.team.team_code == 
self.home_team_code
                end
                stats
        end
end

I understand that this isn't the most efficient or elegant way to do
things. I gleaned this  most recently from the posting at (see his
Case 3):

http://www.therailsway.com/tags/has_many

So, I would like to do something like the following, but I'm having a
problem with the conditions:

        has_many :visitor_stats, :class_name=>"PlayerStat", :conditions=>
<what???>

the condition here should be something matching "player.team.team_code
== self.visiting_team_code" in the code I have now.

        has_many :home_stats, :class_name=>"PlayerStat", :conditions=>
<what???>

and here it should be something matching "player.team.team_code ==
self.home_team_code" in the existing code.

The conditions specified need to be something that could go in a SQL
statement, right? How would I make the conditions work thru the
associations of a PlayerStat belonging to a Player belonging to a Team
which has a team_code. And matching it to this Game's
visiting_team_code or home_team_code




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