简体   繁体   English

列出所有唯一关联活动记录

[英]List all unique associations active record

I have a class named Job and one named Hour 我有一个名为Job的类,一个名为Hour的类

Job has many Hours 工作有很多小时

At some point I have selected a collection of Hours. 在某些时候,我选择了一系列小时。 Now I want to get a list of all the Jobs these hours belongs to. 现在我想获得这些时间所属的所有工作的列表。

There are many ways to get hours. 有很多方法可以获得时间。

If your collection is on AR relation then you can do this 如果您的收藏是AR关系,那么您可以这样做

<hours collection>.includes(:job).map(&:job)

If it is array of hours then simply do 如果它是几小时的数组,那么就这么做

<hours collection>.map(&:job)

You can also achieve this in sub query(hours collection should be on AR relation) 你也可以在子查询中实现这个(小时集合应该在AR关系上)

Job.includes(:hours).where(:id => <hours collection>.select("job_id"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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