Activerecord连接语法。方括号和花括号?
这来自Rails文档: 12.1.3.2加入嵌套关联(多级) 这将产生: 或者,用英语说:“返回所有有文章的类别,这些文章有客人的评论,这些文章也有标签。” 所以这一切都是有道理的。但是如何在ActiveRecord中获取此sql: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
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] })