是否有可能使用rails做这样的查询?SELECT ... AS ...与ActiveRecord
SELECT items.*, (select count(*) from ads where item_id = items.id) as adscount WHERE 1
然后像这样访问这个字段?
@item.adscount
例如,对于每个项目,都有大约100个广告。 而在项目索引视图中,我需要显示每个项目有多少广告。
现在我只发现了如何做一个唯一的查询为每个项目,例如:
select count(*) from ads where item_id = 1
select count(*) from ads where item_id = 2
select count(*) from ads where item_id = 3
etc
编辑:
提出一项反缓存列。
这给了我巨大的性能提升。
控制器做到这一点我敢肯定,99.99% Item.ads.count将生成您需要的查询。 (其中广告belongs_to项目和项目has_many广告 – iced
其实你只需要'item.rb'模型中的'has_many:ads'部分和'@ item.ads.count'就可以做到这一点(你只需要'belongs_to :项目',如果你打算通过广告访问该项目) –
是的,@ item.ads.count会计算每个项目的广告,但如果我有100个项目 - 这是100个查询,这不是最好的。几乎觉得我应该为Items表制作一个Counter缓存列 – user1885058