Why does includes
method behaves like select distinct
One blog has many 10 comments.
includes
output remove same record. But find_by_sql
doesn't remove same record. why??
# includes
1.9.3-p551 :023 > Blog.includes(:comments).where(comments: {id: [1..40]})
SQL (0.6ms) SELECT "blogs"."id" AS t0_r0, "blogs"."name" AS t0_r1, "blogs"."created_at" AS t0_r2, "blogs"."updated_at" AS t0_r3, "comments"."id" AS t1_r0, "comments"."blog_id" AS t1_r1, "comments"."created_at" AS t1_r2, "comments"."updated_at" AS t1_r3 FROM "blogs" LEFT OUTER JOIN "comments" ON "comments"."blog_id" = "blogs"."id" WHERE (("comments"."id" BETWEEN 1 AND 40 OR 1=0))
=> [#<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">]
# find_by_sql
1.9.3-p551 :024 > Blog.find_by_sql("select blogs.* from blogs left outer join comments on comments.blog_id = blogs.id where (comments.id between 1 and 40)")
Blog Load (0.3ms) select blogs.* from blogs left outer join comments on comments.blog_id = blogs.id where (comments.id between 1 and 40)
=> [#<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">, #<Blog id: 1, name: nil, created_at: "2016-11-28 15:47:38", updated_at: "2016-11-28 15:47:38">]
Aucun commentaire:
Enregistrer un commentaire