在我部署在Heroku上的Rails App(v:3.1)中,我在内存中获得了更多同一ID的对象。我的Heroku控制台日志:

>> Project.find_all_by_id(92).size
=> 2
>> ActiveRecord::Base.connection.execute('select * from projects where id=92').to_a.size
=> 1

这怎么可能?可能是什么问题呢?

有帮助吗?

解决方案

解决方案

根据您的SQL查询,您的数据库中显然没有重复的条目。

也许 尺寸 或者 长度 您的班级项目中的方法已被覆盖。我努力了 find_all_by_id SQL查询似乎是正确的。

1.9.2-p180 :006 > Script.find_all_by_id(1).size
  Script Load (0.7ms)  SELECT "scripts".* FROM "scripts" WHERE "scripts"."id" = 1
 => 1 

暗示

如果要计算记录,您应该这样做

Script.where(id: 1).size
  (0.8ms)  SELECT COUNT(*) FROM "scripts" WHERE "scripts"."id" = 1
 => 1 

因为,如您所见, 计数由您的数据库执行 而不是红宝石本身。对于十几行,您不会看到差异,但是如果您有数千万或数百万...

其他提示

irb(main):023:0> ActiveRecord::Base.connection.execute('select count(*) from users where    address_id = 22').fetch_hash
=> {"count(*)"=>"4"}
irb(main):024:0> User.find_all_by_address_id(22).size
=> 4

您最好查找有关MySQL ::结果的文档

http://rubydoc.info/gems/mysql/2.8.1/frames

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top