[英]Fastest way to load eager loaded associations in Rails
I have a Contract
which belongs to a Provider
, and I am doing this: 我有一个属于Provider
的Contract
,我正在这样做:
@contracts = Contract.where(provider_id: provider_ids).includes(:provider)
And I want to get the distinct providers: 我想获得不同的提供者:
@providers = @contracts.map(&:provider).uniq
Is there a way to make DB do all the work? 有没有一种方法可以使DB完成所有工作? ie map is making Ruby work. 即地图使Ruby正常工作。
为什么不这样做
Provider.find Contract.select("distinct provider_id")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.