我試圖做這樣的事情來預加載所有數據,所以我不必在每次循環運行時都進行數據庫調用。在Ruby on Rails中預加載數據庫中的數據
# 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
所以基本上我想要做的是加載所有數據,然後在循環中使用它,而無需連接數據庫的其他方式。什麼是最好的方法呢?
在給出任何建議之前,您確實必須提供有關應用程序體系結構的更多信息。它是網絡嗎?移動?窗體形式?如何通過Web服務設計DAL? – dexter