すべてのデータをプリロードするためにこのようなことをしようとしているので、ループが実行されるたびにデータベースを呼び出す必要はありません。
# Get all data and do some eager loading
bets = Bet.find(:all, :include => :team_id)
games = Game.find(:all)
games.each do |game|
bets.find_all_by_user_id(user.id) # Now I want to get the specific bets, but I can't do find() on the returned array
end
そして私が試した他の方法
bets = Bet.where(:user_id => users) # users are an array of ID's
games = Game.find(:all)
games.each do |game|
bets.find_all_by_user_id(user.id) # This works, but it makes a db call every time, which makes the site really slow.
end
つまり、基本的に私がやろうとしているのは、すべてのデータをロードし、データベースに接続することなくループで使用することです。これに対する最善のアプローチは何ですか?