简体   繁体   English

在Rails中加载渴望加载的关联的最快方法

[英]Fastest way to load eager loaded associations in Rails

I have a Contract which belongs to a Provider , and I am doing this: 我有一个属于ProviderContract ,我正在这样做:

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM