返回引用ID计数最高的记录Active Record

时间:2018-11-20 17:28:33

标签: sql ruby-on-rails postgresql activerecord many-to-many

找到工程师人数最多的国家

工程师有一个country_id引用表Countrys,我通过猜测和检查找到了答案,但是我很想知道如何返回拥有最多工程师人数的单个国家,正在寻找将近24小时...请帮助

country = Country.find(34);
  country.engineers.count;
  => 9.

到目前为止,这就是我所得到的

1 个答案:

答案 0 :(得分:1)

我相信在sql上会是这样的:

</configuration>

   <system.serviceModel>

      <services>

        <service behaviorConfiguration="MyService.ServiceBehavior" name="MyService.Documents">
           <endpoint name="MyService.Documents.Endpoint"
                address="https://MyApp.net/Documents.svc"
                behaviorConfiguration="MyService.EndpointBehavior"
                binding="webHttpBinding"
                bindingConfiguration="TransportSecurity"
                contract="MyService.IDocuments" />
        </service>

        <service behaviorConfiguration="MyService.ServiceBehavior" name="MyService.Leads">
           <endpoint name="MyService.Leads.Endpoint"
                address="https://MyApp.net/Leads.svc"
                behaviorConfiguration="MyService.EndpointBehavior"
                binding="webHttpBinding"
                bindingConfiguration="TransportSecurity"
                contract="MyService.ILeads" />
        </service>

     </services>

  </system.serviceModel>

</configuration>

使用AR的适应性略有改变:

async function DeleteFirebaseStorageFolder(directoryName: string) {
    const serviceAccount = require('../secret/adminSdkPrivateKey.json');

    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        storageBucket: 'gs://yourprojectname.appspot.com'
    });

    const bucket = admin.storage().bucket();
    const files = (await bucket.getFiles({ directory: folderName }))[0];

    const Deletions = [];

    files.forEach(file => {
        Deletions.push(bucket.file(file.name).delete());
    })

    await Promise.all(Deletions);

    bucket.getFiles({ directory: folderName }).then((files) => {
        console.log('Remaining number of files: ' + (files[0].length).toString());
    })
}
DeleteFirebaseStorageFolder('myDirectoryName');

参考:

Can I do a max(count(*)) in SQL?

How to find the highest number of times a value is in records?