{"id":2493,"date":"2013-05-08T11:45:51","date_gmt":"2013-05-08T02:45:51","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=2493"},"modified":"2018-11-14T16:33:55","modified_gmt":"2018-11-14T07:33:55","slug":"activerecord-blockwhere","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/05\/08\/activerecord-blockwhere\/","title":{"rendered":"ActiveRecord\u3067Arel\u3092\u4f7f\u3044\u3084\u3059\u304f\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea activerecord-blockwhere\u3092\u4f5c\u3063\u3066\u307f\u305f"},"content":{"rendered":"

\u304a\u4e45\u3057\u3076\u308a\u3067\u3059\u3002\u5bfa\u5ca1\u3067\u3059\u3002
\n\u81ea\u4f5c\u306egem\u30e9\u30a4\u30d6\u30e9\u30eaactiverecord-blockwhere<\/a>\u3092\u4f5c\u3063\u3066\u307f\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n

\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u7d39\u4ecb<\/h2>\n

activerecord-blockwhere\u306f\u3001ActiveRecord\u306b\u691c\u7d22\u6761\u4ef6\u306e\u7d44\u7acb\u3092\u884c\u3046DSL\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
\nDSL\u306fwhere\u306b\u4e0e\u3048\u305f\u30d6\u30ed\u30c3\u30af\u5185\u3067\u8a18\u8ff0\u3067\u304d\u3001Arel\u306e\u6761\u4ef6\u3092\u77ed\u3044\u30b3\u30fc\u30c9\u3067\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/p>\n

ActiveRecord3\u304b\u3089\u5c0e\u5165\u3055\u308c\u305fArel\u306f\u3088\u304f\u51fa\u6765\u305f\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3001\u975e\u5e38\u306b\u5f37\u529b\u306a\u30af\u30a8\u30ea\u69cb\u7bc9\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n

\u3057\u304b\u3057\u3001\u500b\u3005\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304b\u3089\u5229\u7528\u3059\u308b\u5834\u5408\u3001\u5f8c\u8ff0\u3059\u308barel_table\u3084\u8ad6\u7406\u6f14\u7b97\u306e\u95a2\u4fc2\u3067\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u304c\u7169\u96d1\u306b\u306a\u308b\u305f\u3081\u656c\u9060\u3057\u304c\u3061\u3067\u3057\u305f\u3002
\n\u3082\u3063\u3068\u7c21\u6f54\u306b\u4f7f\u3048\u308c\u3070\u3044\u3044\u306e\u306b\u3001\u3082\u3063\u305f\u3044\u306a\u3044\u306a\uff5e\u3002\u3068\u5e38\u3005\u601d\u3063\u3066\u3044\u305f\u306e\u3067\u3059\u3002<\/p>\n

\u305d\u3093\u306a\u6642\u3001Ruby2.0\u306eRefinements\u3092\u5229\u7528\u3057\u305f activerecord-refinements<\/a> \u306b\u51fa\u4f1a\u3044\u307e\u3057\u305f\u3002
\n\uff08Refinements\u306e\u4ed5\u69d8\u5909\u66f4\u306b\u3088\u308a\u3001\u60dc\u3057\u304f\u3082\u6b63\u5f0f\u7248\u3067\u306f\u4f7f\u3048\u306a\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u304c\u2026\uff09<\/p>\n

\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u304a\u304b\u3052\u3067\u3001\u300cwhere\u306e\u30d6\u30ed\u30c3\u30af\u5f15\u6570\u306f\u7a7a\u3044\u3066\u308b\u3093\u3060\uff01\u300d\u3068\u3044\u3046\u4e8b\u5b9f\u306b\u6c17\u4ed8\u304d\u3001\u52e2\u3044\u306b\u4efb\u305b\u3066\u4f5c\u3063\u305f\u306e\u304cactiverecord-blockwhere\u3067\u3059\u3002<\/p>\n

\u767a\u60f3\u7684\u306b\u3042\u308a\u304c\u3061\u306a\u306e\u3067\u3001\u65e2\u51fa\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u88ab\u3063\u3066\u306a\u3044\u304b\u3061\u3087\u3063\u3068\u4e0d\u5b89\u3067\u3059\u3002<\/p>\n

ActiveRecord\u3067\u8907\u96d1\u306a\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u305f\u3044\uff01<\/h2>\n

Hash\u306b\u3088\u308b\u691c\u7d22\u6761\u4ef6\u306e\u6307\u5b9a<\/h3>\n

ActiveRecord\u306ewhere\u306f\u3001\u5f15\u6570\u306bHash\u3092\u4e0e\u3048\u3066\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

> Person.where(id: 1).to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE \"people\".\"id\" = 1<\/pre>\n

\u5024\u306b\u914d\u5217\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u3001IN\u3067\u691c\u7d22\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n

> Person.where(id: [1,2,3]).to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE \"people\".\"id\" IN (1, 2, 3)<\/pre>\n

\u3057\u304b\u3057\u3001NOT\u3084OR\u3001LIKE\u306a\u3069\u306e\u691c\u7d22\u3092\u884c\u304a\u3046\u3068\u3057\u305f\u5834\u5408\u3001Hash\u3067\u4e0e\u3048\u308b\u6761\u4ef6\u3067\u306f\u5b9f\u73fe\u51fa\u6765\u307e\u305b\u3093\u3002
\n\u305d\u3093\u306a\u5834\u5408\u3001where\u306bSQL\u69cb\u6587\u3068bind\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4e0e\u3048\u308b\u3053\u3068\u3067\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002<\/p>\n

OR\u3092\u542b\u3080\u6761\u4ef6 SQL\u7248<\/h3>\n
> Person.where('id NOT IN (?) OR name LIKE ?', [1,2,3], '%taro%').to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE (id NOT IN (1,2,3) OR name LIKE '%taro%')<\/pre>\n

\u89e3\u6c7a\u306f\u3057\u307e\u3057\u305f\u304c\u3001\u3053\u306e\u8a18\u8ff0\u65b9\u6cd5\u306fSQL\u306e\u4e00\u90e8\u304c\u30ed\u30b8\u30c3\u30af\u306b\u73fe\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3042\u307e\u308a\u7dba\u9e97\u306a\u3084\u308a\u65b9\u3068\u306f\u8a00\u3048\u307e\u305b\u3093\u3002<\/p>\n

\u691c\u7d22\u65b9\u6cd5\u306b\u3088\u3063\u3066\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5909\u3048\u308b(Hash\u3068SQL\u3092\u4f7f\u3044\u5206\u3051\u308b)\u3068\u3044\u3046\u65b9\u6cd5\u3082\u611f\u5fc3\u3057\u307e\u305b\u3093\u3002
\n\u4eca\u307e\u3067Hash\u3067\u4e0e\u3048\u3066\u3044\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3001NOT\u6761\u4ef6\u3092\u4e00\u3064\u8ffd\u52a0\u3059\u308b\u305f\u3081\u306bSQL\u3068bind\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u66f8\u304d\u76f4\u3059\u2026\u306a\u3093\u3066\u4f5c\u696d\u306f\u3084\u308a\u305f\u304f\u306a\u3044\u3067\u3059\u3088\u306d\u3002<\/p>\n

\u8907\u96d1\u306a\u30af\u30a8\u30ea\u306fArel\u306b\u4efb\u305b\u308d\uff1f<\/h2>\n

OR\u3092\u542b\u3080\u6761\u4ef6 Arel\u7248<\/h3>\n

\u8907\u96d1\u306a\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u305f\u3044\u3002\u3067\u3082SQL\u3068bind\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u4f7f\u3044\u305f\u304f\u306a\u3044\u3002
\n\u305d\u3093\u306a\u6642\u306b\u306fActiveRecord(3\u4ee5\u964d)\u306b\u304a\u3044\u3066SQL\u69cb\u7bc9\u3092\u62c5\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3001Arel\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n

\u5148\u307b\u3069\u306e\u6761\u4ef6\u3082Arel\u3092\u4f7f\u3048\u3070\u3053\u3093\u306a\u306b\u98a8\u306b\u30b7\u30f3\u30d7\u30eb\u2015\u2015<\/p>\n

> Person.where(Person.arel_table[:id].not_in([1,2,3]).\r\n   or(Person.arel_table[:name].matches('%taro%'))).to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE \r\n    ((\"people\".\"id\" NOT IN (1, 2, 3) OR \"people\".\"name\" LIKE '%taro%'))<\/pre>\n

\u2026\u306b\u66f8\u304f\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n

Arel\u306eAttribute\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u306b\u306farel_table\u3092\u7d4c\u7531\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u3069\u3046\u3057\u3066\u3082\u5197\u9577\u306a\u30b3\u30fc\u30c9\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002
\n\u307e\u305f\u3001or\u3084and\u306a\u3069\u306e\u8ad6\u7406\u6f14\u7b97\u3092\u4f7f\u3046\u3068\u30e1\u30bd\u30c3\u30c9\u547c\u3073\u51fa\u3057\u306e\u30cd\u30b9\u30c8\u304c\u6df1\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3082\u53ef\u8aad\u6027\u3092\u4e0b\u3052\u308b\u8981\u56e0\u3067\u3059\u306d\u3002<\/p>\n

activerecord-blockwhere\u3092\u4f7f\u3046<\/h2>\n

OR\u3092\u542b\u3080\u6761\u4ef6 blockwhere\u7248<\/h3>\n

activerecord-blockwhere\u3092\u5c0e\u5165\u3059\u308b\u3068\u3001\u5148\u307b\u3069\u306e\u6761\u4ef6\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u51fa\u6765\u307e\u3059\u3002<\/p>\n

> Person.where{ id.not_in([1,2,3]) | name.matches('%taro%') }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE\r\n    ((\"people\".\"id\" NOT IN (1, 2, 3) OR \"people\".\"name\" LIKE '%taro%'))<\/pre>\n

\u304b\u306a\u308a\u76f4\u611f\u7684\u306b\u306a\u3063\u305f\u6c17\u304c\u3057\u307e\u305b\u3093\u304b\uff1f<\/p>\n

\u30d6\u30ed\u30c3\u30af\u5185\u3067\u306f Person.arel_table[:id] \u306e\u4ee3\u308f\u308a\u306b id \u3067Arel\u306eAttribute\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002
\n\u8ff0\u8a9e\uff08\u6761\u4ef6\u6f14\u7b97\u5b50\uff09\u3068\u3057\u3066\u5229\u7528\u3067\u304d\u308b\u30e1\u30bd\u30c3\u30c9\u306fArel\u306e\u3082\u306e\u306a\u306e\u3067\u3001\u4ee5\u4e0b\u30b3\u30fc\u30c9\u3067\u78ba\u8a8d\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n

Arel::Predications\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9\u4e00\u89a7<\/h3>\n
> puts Arel::Predications.instance_methods(false)\r\nnot_eq\r\nnot_eq_any\r\nnot_eq_all\r\neq\r\neq_any\r\neq_all\r\nin\r\nin_any\r\nin_all\r\nnot_in\r\nnot_in_any\r\nnot_in_all\r\nmatches\r\nmatches_any\r\nmatches_all\r\ndoes_not_match\r\ndoes_not_match_any\r\ndoes_not_match_all\r\ngteq\r\ngteq_any\r\ngteq_all\r\ngt\r\ngt_any\r\ngt_all\r\nlt\r\nlt_any\r\nlt_all\r\nlteq\r\nlteq_any\r\nlteq_all<\/pre>\n

\u203b\u53c2\u8003: Arel::Predications<\/a><\/p>\n

\u8ff0\u8a9e\u30b5\u30f3\u30d7\u30eb<\/h3>\n
# eq\r\n> Person.where{ name.eq('taro') }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE \"people\".\"name\" = 'taro'\r\n\r\n# not_eq\r\n> Person.where{ id.not_eq(1) }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE (\"people\".\"id\" != 1)\r\n\r\n# gt\r\n> Person.where{ id.gt(100) }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE (\"people\".\"id\" > 100)\r\n\r\n# matches_any\r\n> Person.where{ name.matches_any(['%taro%', '%hanako%']) }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"\r\n     WHERE ((\"people\".\"name\" LIKE '%taro%' OR \"people\".\"name\" LIKE '%hanako%'))<\/pre>\n

\u307e\u305f\u3001\u6761\u4ef6\u306e\u7d50\u5408\u306a\u3069\u3092\u7c21\u6f54\u306b\u8a18\u8ff0\u3059\u308b\u305f\u3081\u3001Arel\u3092\u62e1\u5f35\u3057\u3066\u300cand or not\u300d\u306b\u5bfe\u5fdc\u3059\u308b\u6f14\u7b97\u5b50\u300c& | !\u300d\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u8ad6\u7406\u6f14\u7b97\u5b50\u30b5\u30f3\u30d7\u30eb<\/h3>\n
# and\r\n> Person.where{ id.gt(50) & id.lt(100) }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"  WHERE (\"people\".\"id\" > 50 AND \"people\".\"id\" < 100)\r\n\r\n# or\r\n> Person.where{ name.eq('suzuki taro') | name.matches('%ichiro%') }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"\r\n     WHERE ((\"people\".\"name\" = 'suzuki taro' OR \"people\".\"name\" LIKE '%ichiro%'))\r\n\r\n# not\r\n> Person.where{ !(id.eq(10) & name.matches('jiro')) }.to_sql\r\n=> SELECT \"people\".* FROM \"people\"\r\n     WHERE (NOT (\"people\".\"id\" = 10 AND \"people\".\"name\" LIKE 'jiro'))<\/pre>\n

\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304b\u3089\u76f4\u63a5\u4f7f\u3046\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c\u3001\u8907\u96d1\u306a\u6761\u4ef6\u3092scope\u3084\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u306b\u307e\u3068\u3081\u308b\u969b\u306a\u3069\u306b\u3082\u6709\u7528\u3067\u3059\u3002
\n\u3088\u304b\u3063\u305f\u3089\u4e00\u5ea6\u304a\u8a66\u3057\u304f\u3060\u3055\u3044\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u304a\u4e45\u3057\u3076\u308a\u3067\u3059\u3002\u5bfa\u5ca1\u3067\u3059\u3002
\n\u81ea\u4f5c\u306egem\u30e9\u30a4\u30d6\u30e9\u30eaactiverecord-blockwhere\u3092\u4f5c\u3063\u3066\u307f\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002
\n\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u7d39\u4ecb
\nactiverecord-blockwhere\u306f\u3001ActiveRecord\u306b\u691c\u7d22\u6761\u4ef6\u306e\u7d44\u7acb\u3092\u884c\u3046DSL\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[96,22,24,95],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2493"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=2493"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2493\/revisions"}],"predecessor-version":[{"id":17937,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2493\/revisions\/17937"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=2493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=2493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=2493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}