Translate

Wednesday, 2 March 2016

Difference between Include and Joins

:join :- It joins table together in sql. eg; Category.joins(:articles) SELECT categories.* FROM categories INNER JOIN articles ON articles.category_id = categories.id :includes :- Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries. - It is a solution of n+1 query eg:- Article.includes(:comments).where(comments: { visible: true }) SELECT "articles"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5 FROM "articles" LEFT OUTER JOIN "comments" ON "comments"."article_id" = "articles"."id" WHERE (comments.visible = 1) I

1 comment: