我试图做这样的事情来预加载所有数据,所以我不必在每次循环运行时都进行数据库调用。在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