Skip to content

Instantly share code, notes, and snippets.

@sebastian-palma
Created March 29, 2021 12:08
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 sebastian-palma/37b545252652b1772c61e43b1a069369 to your computer and use it in GitHub Desktop.
Save sebastian-palma/37b545252652b1772c61e43b1a069369 to your computer and use it in GitHub Desktop.
[1] pry(main)> Site.includes(:site_links).where.not(site_links: { active: true })
SQL (0.7ms) SELECT "sites"."id" AS t0_r0, "sites"."name" AS t0_r1, "sites"."created_at" AS t0_r2, "sites"."updated_at" AS t0_r3, "site_links"."id" AS t1_r0, "site_links"."site_id" AS t1_r1, "site_links"."link_id" AS t1_r2, "site_links"."active" AS t1_r3, "site_links"."created_at" AS t1_r4, "site_links"."updated_at" AS t1_r5 FROM "sites" LEFT OUTER JOIN "site_links" ON "site_links"."site_id" = "sites"."id" WHERE "site_links"."active" != $1 [["active", true]]
=> [#<Site:0x0000560ffba92250 id: 1, name: "first", created_at: Sun, 28 Mar 2021 18:46:03.661861000 UTC +00:00, updated_at: Sun, 28 Mar 2021 18:46:03.661861000 UTC +00:00>,
#<Site:0x0000560ffba90428 id: 2, name: "second", created_at: Sun, 28 Mar 2021 18:46:06.263937000 UTC +00:00, updated_at: Sun, 28 Mar 2021 18:46:06.263937000 UTC +00:00>]
[2] pry(main)> Site.includes(:site_links).where.not("site_links.active = true")
Site Load (1.0ms) SELECT "sites".* FROM "sites" WHERE NOT (site_links.active = true)
Site Load (0.8ms) SELECT "sites".* FROM "sites" WHERE NOT (site_links.active = true) /* loading for inspect */ LIMIT $1 [["LIMIT", 11]]
=> #<Site::ActiveRecord_Relation:0x2bd4>
@sebastian-palma
Copy link
Author

Rails::VERSION::STRING
=> "6.1.3.1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment