[Rails] Re: Noob - Associated Tables Find Query

Jan Prill
Thu, 12 Jan 2006 16:50:33 -0000

Hi, Peter,

try something like (untested):

Prediction.find(:all, :include => :match, :conditions =>
"predictions.user_id = #{my_user_id} AND matches.match_date >=
''#{my_formatted_time}''")

and have a look at examples in the test suite of
http://dev.rubyonrails.org/browser/trunk/activerecord/test/associations_go_eager_test.rb

best regards
Jan Prill

Peter Baldwin wrote:

>Anyone able to help with this? Assistance would be much appreciated.
>Pete
>
>
>
>
>>I''m trying to write a football prediction game.
>>
>>I have a Matches table and a Predictions table.
>>Each Match :has_many Predictions, and each Prediction :belongs_to a
>>Match
>>
>>Each Match has (amongst other things) a Match.match_date field.
>>Each Prediction has (amongst other things) a User_id field
>>
>>I''m trying to get a list of all Predictions a particular User
has made
>>which "belong to" Matches for an arbitrary time period (e.g.
"Time.now
>><= now()")
>>e.g. All "User 1"''s predictions relating to matches
from last week.
>>
>>Given that the tables are associated, is it possible to do a single
>>.find query stipulating both the Prediction.User_id and the
>>Match.match_date, which returns a list of a user''s predictions
for the
>>period?
>>
>>
>
>
>
>
>