find_by_id(params[:game_id])
@events = Event.find(:all,
:select=>'event, ' <<
'AVG(time)/1000 as average_time',
:group=>'events.event ASC',
:joins=>' INNER JOIN plays ON events.play_id=plays.id',
:conditions=>["plays.game_id = ? AND plays.player_id= ?",
@game.id, @player.id]
).map { |event|
{:event=>event.event,
:average_time=>event.average_time.to_i}
}
respond_to do |format|
format.html { render :layout=>false if request.xhr? }
format.text { render :layout=>false }
format.xml { render :xml=>{'player'=>@player,
'game'=>@game,
'events'=>@events
}.to_xml(:root=>'player_performance_report',
:skip_types=>true) }
end
end
end
This code sets the @player and @game variables, which allow the view to know which
player and game were selected and display the information, and then it prepares the
data. It retrieves the performance data using SQL that is similar to the example at the end
of Chapter 4, but it uses find to retrieve the values instead of find_by_sql. The routine
CHAPTER 5 n CONNECTING YOUR REPORTS TO THE WORLD 103
then maps it into an array of hashes, with each hash having an event value and an
average_time value.
Pages:
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160