Skip to content

Instantly share code, notes, and snippets.

@brunvez
Last active June 4, 2020 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunvez/0b07f828da222441ffc9a5c44ef30f1e to your computer and use it in GitHub Desktop.
Save brunvez/0b07f828da222441ffc9a5c44ef30f1e to your computer and use it in GitHub Desktop.
N + 2 queries
class Post < ApplicationRecord
has_many :comments
end
class Comment < ApplicationRecord
belongs_to :post
end
class Post < ApplicationRecord
has_many :comments
has_many :uncensored_comments, -> { where(censored: false) }, class_name: 'Comment'
# rails will automatically limit the number of records for us
has_one :most_liked_comment, -> { order(likes: :desc) }, class_name: 'Comment'
end
class Post < ApplicationRecord
has_many :comments
# we need to specify a new name, a lambda to filter the comments and the model class name
has_many :uncensored_comments, -> { where(censored: false) }, class_name: 'Comment'
end
Post Load (0.4ms) SELECT "posts".* FROM "posts"
Comment Load (0.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 ORDER BY "comments"."likes" DESC LIMIT $2 [["post_id", 1], ["LIMIT", 1]]
Comment Load (0.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 ORDER BY "comments"."likes" DESC LIMIT $2 [["post_id", 2], ["LIMIT", 1]]
Comment Load (0.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 ORDER BY "comments"."likes" DESC LIMIT $2 [["post_id", 3], ["LIMIT", 1]]
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 ORDER BY "comments"."likes" DESC LIMIT $2 [["post_id", 4], ["LIMIT", 1]]
Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 ORDER BY "comments"."likes" DESC LIMIT $2 [["post_id", 5], ["LIMIT", 1]]
Post Load (0.4ms) SELECT "posts".* FROM "posts"
Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" IN ($1, $2, $3, $4, $5) ORDER BY "comments"."likes" DESC [["post_id", 1], ["post_id", 2], ["post_id", 3], ["post_id", 4], ["post_id", 5]]
Post Load (0.5ms) SELECT "posts".* FROM "posts"
Comment Load (1.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" IN ($1, $2, $3, $4, $5) [["post_id", 1], ["post_id", 2], ["post_id", 3], ["post_id", 4], ["post_id", 5]]
Comment Load (6.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 1], ["censored", false]]
Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 2], ["censored", false]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 3], ["censored", false]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 4], ["censored", false]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 5], ["censored", false]]
Post Load (0.5ms) SELECT "posts".* FROM "posts"
Comment Load (6.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 1], ["censored", false]]
Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 2], ["censored", false]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 3], ["censored", false]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 4], ["censored", false]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 AND "comments"."censored" = $2 [["post_id", 5], ["censored", false]]
Post Load (0.5ms) SELECT "posts".* FROM "posts"
Comment Load (0.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."censored" = $1 AND "comments"."post_id" IN ($2, $3, $4, $5, $6) [["censored", false], ["post_id", 1], ["post_id", 2], ["post_id", 3], ["post_id", 4], ["post_id", 5]]
Post Load (0.5ms) SELECT "posts".* FROM "posts"
Comment Load (1.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" IN ($1, $2, $3, $4, $5) [["post_id", 1], ["post_id", 2], ["post_id", 3], ["post_id", 4], ["post_id", 5]]
Post Load (0.5ms) SELECT "posts".* FROM "posts"
Comment Load (0.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 1]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 2]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 3]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 4]]
Comment Load (0.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 5]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment