Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord连接语法。方括号和花括号?_Activerecord - Fatal编程技术网

Activerecord连接语法。方括号和花括号?

Activerecord连接语法。方括号和花括号?,activerecord,Activerecord,这来自Rails文档: 12.1.3.2加入嵌套关联(多级) 这将产生: 或者,用英语说:“返回所有有文章的类别,这些文章有客人的评论,这些文章也有标签。” 所以这一切都是有道理的。但是如何在ActiveRecord中获取此sql: SELECT categories.* FROM categories INNER JOIN articles ON articles.category_id = categories.id INNER JOIN comments ON co

这来自Rails文档:

12.1.3.2加入嵌套关联(多级)

这将产生:

或者,用英语说:“返回所有有文章的类别,这些文章有客人的评论,这些文章也有标签。”

所以这一切都是有道理的。但是如何在ActiveRecord中获取此sql:

SELECT categories.* FROM categories
      INNER JOIN articles ON articles.category_id = categories.id
      INNER JOIN comments ON comments.article_id = articles.id
      INNER JOIN guests ON guests.comment_id = comments.id
      INNER JOIN tags ON tags.comments_id = comments.id
如何加入
标记
返回
注释
。用英语,我想:

返回包含文章的所有类别,其中这些文章包含来宾的评论,并且这些评论还包含标签


更重要的是,用什么样的方式来看待方括号和花括号?

我想这就是你想要的:

Category.joins(articles: { comments: [:guest, :tag] })
将大括号(散列)视为联接表的嵌套条件。将方括号(数组)视为连接多个表的方法

SELECT categories.* FROM categories
      INNER JOIN articles ON articles.category_id = categories.id
      INNER JOIN comments ON comments.article_id = articles.id
      INNER JOIN guests ON guests.comment_id = comments.id
      INNER JOIN tags ON tags.comments_id = comments.id
Category.joins(articles: { comments: [:guest, :tag] })