Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个名为 Job 的班级和一个名为 Hour 的班级
工作有很多小时
在某些时候,我选择了 Hours 的集合。现在我想获取这些时间所属的所有工作的列表。
有很多方法可以获得小时数。
如果您的收藏与 AR 相关,那么您可以这样做
<hours collection>.includes(:job).map(&:job)
如果是几个小时,那么只需执行
<hours collection>.map(&:job)
您也可以在子查询中实现这一点(小时集合应该在 AR 关系上)
Job.includes(:hours).where(:id => <hours collection>.select("job_id"))