加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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)}
    )}
  )
)

这是非常火腿(如果它甚至有效).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读