我有一个带有mysql数据库的rails应用程序。
我有2个型号: [foo,bar] 其中 foo has_many bar
$ rails c
foo.all
SELECT `foo`.* FROM `foo`
SELECT COUNT(*) FROM `foo` WHERE `foo`.`id` = 1
SELECT COUNT(*) FROM `foo` WHERE `foo`.`id` = 2
SELECT COUNT(*) FROM `foo` WHERE `foo`.`id` = 3
SELECT COUNT(*) FROM `foo` WHERE `foo`.`id` = 4
SELECT COUNT(*) FROM `foo` WHERE `foo`.`id` = 5
EXPLAIN SELECT COUNT(*) FROM `foo` WHERE `bar`.`id` = 1
EXPLAIN SELECT COUNT(*) FROM `foo` WHERE `bar`.`id` = 2
EXPLAIN SELECT COUNT(*) FROM `foo` WHERE `bar`.`id` = 3
EXPLAIN SELECT COUNT(*) FROM `foo` WHERE `bar`.`id` = 4
EXPLAIN SELECT COUNT(*) FROM `foo` WHERE `bar`.`id` = 5
=> [...]
在我的应用程序中,我需要迭代所有这些记录,我不希望所有这些额外的sql语句。我只需要第一个SQL语句提供的数据。
关联存在,但我是ajax动态的其余内容,因此不需要像ActiveRecord那样做任何“预加载”。