ruby-on-rails – 嵌套连接查询,仅第三个表的最后一行
发布时间:2020-12-17 02:10:04 所属栏目:百科 来源:网络整理
导读:所以我一直在用这个查询挣扎数小时,我真的不知道如何得到我需要的结果.也许你们有些人有线索? 到目前为止,这是我的查询: items = Item.joins(projects: :connexions) .where.not(connexions: []).where("connexions.updated_at ?",3.years.ago) 它工作正常
所以我一直在用这个查询挣扎数小时,我真的不知道如何得到我需要的结果.也许你们有些人有线索?
到目前为止,这是我的查询: items = Item.joins(projects: :connexions) .where.not(connexions: []).where("connexions.updated_at > ?",3.years.ago) 它工作正常.但我需要的是项目,其中项目的最后连接“updated_at”大于3年前. 基本上,我想做一个items.projects.connexions.last.where等. 你有一些提示吗? 谢谢 :) 编辑:为了更具体地说明我的“连接”规范,我需要检索3年以来没有连接的项目的记录.这就是我使用updated_at字段的原因! 编辑2:我设法使用jvillian答案. items = Item.joins(:projects).where(projects: {id: Project.joins(:connexions).where( connexions: { id: Connexion.order(updated_at: :desc).limit(1).where("updated_at < ?",3.years.ago)} )} ) 谢谢 ! 解决方法
这可能对你有用(未经测试)
Item.where.not(id: Item.joins(:projects).where( projects: {id: Project.joins(:connections).where( connections: {id: Connection.where("updated_at <= ?",3.years.ago)} )} ) ) 这是非常火腿(如果它甚至有效). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- React----React应用程序流式服务端渲染
- c# – 创建封装Generic Collection的类有缺点吗?
- swift3 – 没有”候选人产生预期的上下文结果类型’NSNumbe
- 单元测试 – 如何编写没有这么多嘲讽的测试?
- 如何在SQLite中创建自增字段
- Error parsing XML: unbound prefix on library 解决方法
- 【ASP.NET】利用.ashx、Ajax、Session优化Repeater的使用
- 在Oracle SQL Developer中显示完整的SDO_DIM_ARRAY
- c# – 通过AJAX将WIF发送到单独的域
- c# – SendKeys.Send(“^ V”);不粘贴图像,但粘贴文本