Rails5 ã® or ã¯ã¨ãªããã°ãèªçºãããã§è¶ æã
Rails5 ã«ãªã£ã¦ ActiveRecord ã« or ã¯ã¨ãªãå°å ¥ããã¾ãããæ©éãæ¢åã®ã³ã¼ããæ¸ãæãããã¨ãããã§ãããããªãæ éã«ä½¿ããªãã¨çµåé åºã®ã¯ãã«åµã£ã¦ãã°ãããã§æãã§ãã
ãã®ãããªï¼éã®æããï¼ã³ã¼ãããã£ãã¨ãã¾ãã
Country. where(id: 1). where("id = ? OR id = ?", 1, 2). pluck(:id)
çæããã SQL ã¯ããã§ã
SELECT "countries"."id" FROM "countries" WHERE "countries"."id" = $1 AND (id = 1 OR id = 2) [["id", 1]]
å®è¡çµæã¯ããã
[ [0] 1 ]
ããããor ã¯ã¨ãªãç¨ãã¦ç´ ç´ã«æ¸ãæãã¦ã¿ã¾ãã
Country. where(id: 1). where(id: 1).or(Country.where(id: 2)). pluck(:id)
çæããã SQL ã¯ããã§ã
SELECT "countries"."id" FROM "countries" WHERE ("countries"."id" = $1 AND "countries"."id" = $2 OR "countries"."id" = $3) [["id", 1], ["id", 1], ["id", 2]]
å®è¡çµæã¯ãããã¦ããã£ãã¨éãããªãããï¼ï¼
[ [0] 1, [1] 2 ]
æåã®ã±ã¼ã¹ã§ã¯ A AND (B OR C)
ã®ããã« OR å¥ã®ã¾ãããæ示çã«ã«ãã³ã§å²ã¾ãã¦ããã®ã«å¯¾ãã
or ã¯ã¨ãªã使ã£ãã±ã¼ã¹ã§ã¯ A AND B OR C
ã¨ã«ãã³ããªããçµæçã« (A AND B) OR C
ã¨è©ä¾¡ãããããã§ãã
æåã®ã±ã¼ã¹ã¨åæ§ã®çµæãå¾ãã«ã¯ãor ã¯ã¨ãªã使ã£ãè¡ãæåã«æã£ã¦ããã¨ããããã§ãã
Country. where(id: 1).or(Country.where(id: 2)). # ãã®è¡ã¨ where(id: 1). # ãã®è¡ãå ¥ãæ¿ãã pluck(:id)
çæããã SQL ã¯ãããªãã
SELECT "countries"."id" FROM "countries" WHERE ("countries"."id" = $1 OR "countries"."id" = $2) AND "countries"."id" = $3 [["id", 1], ["id", 2], ["id", 1]]
å®è¡çµæã¯æåã¨çãããªãã¾ããã
[ [0] 1 ]
(B OR C) AND A
ã®ããã« OR å¥ã®ã¾ãããã«ãã³ã§å²ã¾ãã¦ãæåã®ã±ã¼ã¹ã® A AND (B OR C)
ã¨ç価ã«ãªã£ã¦ãã¾ãã
or ã¯ã¨ãªãæãã§ãããã
ãã®æåããã£ããç解ããä¸ã§ã³ã¼ããæ¸ããã¨ãã¦ãããã¼ã ã®å¥ã®ã¨ã³ã¸ãã¢ãï¼ãããã¯å°æ¥ã®èªåãï¼ç¡èªè¦ã« where å¥ã追å ãããé çªãå ¥ãæ¿ããããã¦ãäºæãã¬ãã°ãçã¾ããããããâ¦ã
æ éã«ä½¿ã£ãã»ããããããã§ãã