Skip to content

Latest commit

 

History

History
29 lines (20 loc) · 1.02 KB

joins.md

File metadata and controls

29 lines (20 loc) · 1.02 KB

Joins in rails

SQL Joins

inner join

joins

User.joins(:notification_info).where(notification_infos: {optivo_blacklisted: [false, nil], optivo_bounced: [false, nil]})

becomes

SELECT "users".* FROM "users" INNER JOIN "notification_infos" ON "notification_infos"."user_id" = "users"."id" WHERE ("notification_infos"."optivo_blacklisted" = 'f' OR "notification_infos"."optivo_blacklisted" IS NULL) AND ("notification_infos"."optivo_bounced" = 'f' OR "notification_infos"."optivo_bounced" IS NULL)

outer join

includes

User.includes(:notification_info).where(notification_infos: {optivo_blacklisted: [false, nil], optivo_bounced: [false, nil]})

becomes

SELECT ... FROM "users" LEFT OUTER JOIN "notification_infos" ON "notification_infos"."user_id" = "users"."id" WHERE ("notification_infos"."optivo_blacklisted" = 'f' OR "notification_infos"."optivo_blacklisted" IS NULL) AND ("notification_infos"."optivo_bounced" = 'f' OR "notification_infos"."optivo_bounced" IS NULL)