{"id":108,"date":"2021-11-29T09:00:00","date_gmt":"2021-11-29T00:00:00","guid":{"rendered":"https:\/\/www.miracleave.co.jp\/contents\/?p=108"},"modified":"2023-05-23T11:34:14","modified_gmt":"2023-05-23T02:34:14","slug":"clean-activesupport-specify-the-period","status":"publish","type":"post","link":"https:\/\/www.miracleave.co.jp\/contents\/108\/clean-activesupport-specify-the-period\/","title":{"rendered":"\u3010rails\u3011ActiveSupport\u306e\u671f\u9593\u6307\u5b9a\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u30b3\u30fc\u30c9\u304c\u30b9\u30c3\u30ad\u30ea\u3057\u305f\u8a71"},"content":{"rendered":"\n<div class=\"wp-block-cocoon-blocks-icon-box common-icon-box block-box information-box\">\n\n<p>\u8a18\u4e8b\u4f5c\u6210\u304b\u30891\u5e74\u4ee5\u4e0a\u7d4c\u904e\u3057\u3066\u3044\u307e\u3059\u3002<br>\u5185\u5bb9\u304c\u53e4\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n<\/div>\n\n\n\n<p>\u4eca\u65e5\u306frails\u3067\u30b9\u30d1\u30b2\u30c3\u30c6\u30a3\u30b3\u30fc\u30c9\u3092\u4fbf\u5229\u30e1\u30bd\u30c3\u30c9\u3067\u30ad\u30ec\u30a4\u306b\u3057\u305f\u3088\u3068\u3044\u3046\u8a71\u3067\u3059\u3002 <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u74b0\u5883<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ruby 2.6.6<\/li>\n\n\n\n<li>rails 5.2.3<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u8003<\/h3>\n\n\n\n<p><a href=\"https:\/\/qiita.com\/terufumi1122\/items\/3aa21c20eeacbce33b93\">\u3010\u77e5\u3089\u305a\u306b\u640d\u3057\u305f\u3011ActiveSupport\u306e\u671f\u9593\u6307\u5b9a\u30e1\u30bd\u30c3\u30c9all_day, all_week, all_month, all_year<\/a><br><a href=\"https:\/\/railsguides.jp\/active_support_core_extensions.html\">Active Support \u30b3\u30a2\u62e1\u5f35\u6a5f\u80fd<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4eca\u9031\u306e\u65e5\u66dc\u65e5\u301c\u571f\u66dc\u65e5\u306e\u671f\u9593\u3067\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u6642<\/h3>\n\n\n\n<p>\u4f7f\u3046\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066\u306f<code>beginning_of_week<\/code>\u3068<code>end_of_week<\/code>\u3092\u4f7f\u3063\u3066\u4eca\u9031\u306e\u65e5\u66dc\u65e5\u3068\u4eca\u9031\u306e\u571f\u66dc\u65e5\u3092\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># \u4eca\u9031\u65e5\u66dc\u65e5\uff08\u65e5\u66dc\u59cb\u307e\u308a\uff09\u3092\u53d6\u5f97\u3059\u308b\u30e1\u30bd\u30c3\u30c9\nirb(main):001:0&gt; Date.today.beginning_of_week\n=&gt; 2020-12-06 Dec 2020\n# \u3053\u308c\u3060\u3068\u6708\u66dc\u59cb\u307e\u308a\u3067\u53d6\u5f97\u3057\u3066\u3057\u307e\u3046\u306e\u3067-1\u3057\u307e\u3059\nirb(main):002:0&gt; Date.today.beginning_of_week - 1\n=&gt; 2020-12-07 Dec 2020\n# \u65e5\u66dc\u65e5\u304c\u53d6\u308c\u307e\u3057\u305f\uff01<\/code><\/pre><\/div>\n\n\n\n<p>\u571f\u66dc\u65e5\u3092\u53d6\u5f97\u3057\u307e\u3057\u3087\u3046\u3002<br>\u65e5\u4ed8\u3092\u7bc4\u56f2\u6307\u5b9a\u3059\u308b\u969b\u306b\u7d42\u308f\u308a\u306e\u65e5\u306b\u3061\u306e\u4f55\u6642\u307e\u3067\u3092\u7bc4\u56f2\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f. <code>Time.now<\/code>\u3092\u4f7f\u3063\u3066\u571f\u66dc\u65e5\u306e23:59:59\u307e\u3067\u3092\u6307\u5b9a\u3059\u308b\u7bc4\u56f2\u306b\u542b\u3081\u308b\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3057\u305f. \u3053\u308c\u30670:00:00 ~ 23:59:59\u306e\u305d\u306e\u65e51\u65e5\u307e\u3067\u3092\u6307\u5b9a\u3057\u305f\u65e5\u4ed8\u306e\u7bc4\u56f2\u5185\u306b\u542b\u3081\u308b\u4e8b\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># \u4eca\u9031\u571f\u66dc\u65e5\u3092\u53d6\u5f97\u3059\u308b\u30e1\u30bd\u30c3\u30c9\nirb(main):003:0&gt; Time.now.end_of_week - 1\n=&gt; 2020-12-12 23:59:58 +0900\n# \u571f\u66dc\u65e5\u304c\u53d6\u308c\u307e\u3057\u305f\uff01<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u6b21\u306b\u65e5\u66dc\u65e5\u301c\u571f\u66dc\u65e5\u307e\u3067\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u308b<\/h3>\n\n\n\n<p>\u4eca\u56de\u306fStudy\u30e2\u30c7\u30eb\u3068\u3044\u3046TBL\u306b<code>id<\/code>, <code>time<\/code>, <code>total<\/code>, <code>user_id<\/code>, <code>created_at<\/code>\u30ab\u30e9\u30e0\u3092\u6301\u3064\u5b66\u7fd2\u6642\u9593\u3092\u8a18\u9332\u3059\u308bTBL\u3092\u4f8b\u306b\u8003\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u30ab\u30e9\u30e0<\/th><th>\u30bf\u30a4\u30d7<\/th><\/tr><\/thead><tbody><tr><td>id<\/td><td>digint<\/td><\/tr><tr><td>time<\/td><td>float<\/td><\/tr><tr><td>total<\/td><td>float<\/td><\/tr><tr><td>user_id<\/td><td>integer<\/td><\/tr><tr><td>created_at<\/td><td>datetime<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u3053\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u304c\u5165\u3063\u3066\u3044\u308b\u3068\u60f3\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>irb(main):002:0&gt; Study.all\n  Study Load (0.6ms)  SELECT  `studies`.* FROM `studies` LIMIT 11\n=&gt; #&lt;ActiveRecord::Relation [#&lt;Study id: 2, time: 0.5, total: 0.5, user_id: nil, created_at: &quot;2020-12-05 11:09:38&quot;, updated_at: &quot;2020-12-05 11:09:38&quot;&gt;, #&lt;Study id: 3, time: 1.5, total: 2.0, user_id: nil, created_at: &quot;2020-12-07 11:09:42&quot;, updated_at: &quot;2020-12-07 11:09:42&quot;&gt;, #&lt;Study id: 4, time: 2.0, total: 4.0, user_id: nil, created_at: &quot;2020-12-07 11:09:49&quot;, updated_at: &quot;2020-12-07 11:09:49&quot;&gt;, #&lt;Study id: 5, time: 0.5, total: 4.5, user_id: nil, created_at: &quot;2020-12-07 11:26:38&quot;, updated_at: &quot;2020-12-07 11:26:38&quot;&gt;, #&lt;Study id: 6, time: 1.0, total: 5.5, user_id: nil, created_at: &quot;2020-12-07 11:27:31&quot;, updated_at: &quot;2020-12-07 11:27:31&quot;&gt;, #&lt;Study id: 7, time: 0.5, total: 6.0, user_id: nil, created_at: &quot;2020-12-07 23:16:04&quot;, updated_at: &quot;2020-12-07 23:16:04&quot;&gt;, #&lt;Study id: 8, time: 1.5, total: 7.5, user_id: nil, created_at: &quot;2020-12-07 23:18:49&quot;, updated_at: &quot;2020-12-07 23:18:49&quot;&gt;]&gt;<\/code><\/pre><\/div>\n\n\n\n<p>id2~8\u307e\u3067\u306e7\u4ef6\u306e\u30ec\u30b3\u30fc\u30c9\u304c\u5b58\u5728\u3057\u3066\u3044\u307e\u3059(id1\u304c\u6b6f\u629c\u3051\u3057\u3066\u3044\u308b\u306e\u306f\u7121\u8996\u3057\u3066\u4e0b\u3055\u3044w)<\/p>\n\n\n\n<p>\u3053\u306e\u30c7\u30fc\u30bf\u304c\u3042\u308b\u72b6\u614b\u3067\u5148\u7a0b\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u671f\u9593\u3092\u4eca\u9031\u306b\u7d5e\u3063\u3066\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>irb(main):003:0&gt; Study.where(created_at: Date.today.beginning_of_week - 1..Time.now.end_of_week - 1).sum(:time)\n   (4.1ms)  SELECT SUM(`studies`.`time`) FROM `studies` WHERE `studies`.`created_at` BETWEEN &#39;2020-12-06&#39; AND &#39;2020-12-12 23:59:58&#39;\n=&gt; 7.0<\/code><\/pre><\/div>\n\n\n\n<p>where\u3067created_at\u304c\u4eca\u9031\u65e5\u66dc\u65e5(12\/6)\u301c\u4eca\u9031\u571f\u66dc\u65e5(12\/12)\u307e\u3067\u306e7\u65e5\u9593\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u3001sum\u30e1\u30bd\u30c3\u30c9\u3067time\u30ab\u30e9\u30e0\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b9\u30d1\u30b2\u30c3\u30c6\u30a3\u30b3\u30fc\u30c9\u3092\u66f8\u304f<\/h3>\n\n\n\n<p>\u307e\u305a\u79c1\u304c\u6700\u521d\u306b\u66f8\u3044\u305f\u30b9\u30d1\u30b2\u30c3\u30c6\u30a3\u30b3\u30fc\u30c9\u3067\u66f8\u304d\u307e\u3059w<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># studies_controller.rb\n~\u7701\u7565~\ndef index\n@this_week_total_time = Study.where(created_at: Date.today.beginning_of_week - 1..Time.now.end_of_week - 1).sum(:time)\nend\n~\u7701\u7565~<\/code><\/pre><\/div>\n\n\n\n<p>controller\u306b\u3053\u3093\u306a\u66f8\u3044\u3066\u305f\u3089\u3069\u3093\u3069\u3093fat\u306b\u306a\u3063\u3066\u3044\u304f\u4e88\u611f\u3057\u304b\u3057\u306a\u3044\u3067\u3059\u3057\u3001\u6c5a\u3044\u30b3\u30fc\u30c9\u3067\u3059\u306d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b01<\/h3>\n\n\n\n<p>\u30e2\u30c7\u30eb\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066controller\u306e\u898b\u901a\u3057\u3092\u3088\u304f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># studies_controller.rb\n~\u7701\u7565~\ndef index\n@this_week_total_time = Study.this_week_total\nend\n~\u7701\u7565~\n\n# models \/ study.rb\n~\u7701\u7565~\ndef this_week_total\nself.where(created_at: date.today.beginning_of_week - 1..Time.now.end_of_week - 1).sum(:time)\nend\n~\u7701\u7565~<\/code><\/pre><\/div>\n\n\n\n<p>\u30ed\u30b8\u30c3\u30af\u90e8\u5206\u3092\u30e2\u30c7\u30eb\u306b\u79fb\u884c\u3057\u305f\u306e\u3067\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u304c\u30b9\u30c3\u30ad\u30ea\u3057\u307e\u3057\u305f!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u672c\u984crails\u306e\u4fbf\u5229\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046<\/h3>\n\n\n\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306f\u30ad\u30ec\u30a4\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001\u30e2\u30c7\u30eb\u30e1\u30bd\u30c3\u30c9\u304c\u5197\u9577\u3067\u3059\u3002<br>\u305d\u3053\u3067rails\u304c\u7528\u610f\u3057\u3066\u304f\u308c\u3066\u3044\u308b<code>all_week<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># models \/ study.rb\n~\u7701\u7565~\ndef this_week_total\n@today = Time.now\n+@range = @today.all_week(:sunday) \u2190 \u8ffd\u52a0\nself.where(created_at: @range).sum(:time)\nend\n~\u7701\u7565~<\/code><\/pre><\/div>\n\n\n\n<p>all_week\u306f\u5f15\u6570\u3092\u53d6\u308b\u3053\u3068\u3067\u4efb\u610f\u306e\u66dc\u65e5\u59cb\u307e\u308a\u306b\u3067\u304d\u307e\u3059.\u4eca\u56de\u306f\u65e5\u66dc\u59cb\u307e\u308a\u306b\u3057\u305f\u3044\u306e\u3067<code>(:sunday)<\/code>\u3092\u6e21\u3057\u307e\u3059\u3002<br>\u6700\u521d\u306e\u30b3\u30fc\u30c9\u306b\u6bd4\u3079\u308b\u3068\u5927\u5206\u30ad\u30ec\u30a4\u306a\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3057\u305f!<br>\u4ee5\u524d\u306e\u30b3\u30fc\u30c9\u3060\u3068<code>beginning_of_week - 1<\/code>\u306a\u306e\u3067\u3001\u300c\u9031\u306e\u59cb\u307e\u308a\u306e\u65e5\u4ed8\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u3051\u3069\u3001-1\u3057\u3066\u3044\u308b\u304b\u3089~\u3048\u30fc\u3063\u3068\u3001\u7d50\u5c40\u4f55\u66dc\u65e5\u306a\u306e!?\u300d\u3063\u3066\u4f55\u30f6\u6708\u5f8c\u306e\u81ea\u5206\u304c\u898b\u305f\u3089\u601d\u3046\u3067\u3057\u3087\u3046w<br>\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3057\u305f<code>@range = @today.all_week(:sunday)<\/code>\u3067\u3042\u308c\u3070\u300c\u65e5\u66dc\u59cb\u307e\u308a\u3067\u671f\u9593\u304c\u4eca\u9031\u306a\u3093\u3060\u306a\u300d\u3068\u76f4\u611f\u7684\u306b\u5206\u304b\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u5ff5\u306e\u70ba\u3001\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>irb(main):006:0&gt; @today = Time.now\n=&gt; 2020-12-09 11:25:43 +0900\nirb(main):007:0&gt; @range = @today.all_week(:sunday)\n=&gt; 2020-12-06 00:00:00 +0900..2020-12-12 23:59:59  +0900\nirb(main):008:0&gt; Study.where(created_at: @range).sum(:time)\n   (0.5ms)  SELECT SUM(`studies`.`time`) FROM `studies` WHERE `studies`.`created_at` BETWEEN &#39;2020-12-06 00:00:00&#39; AND &#39;2020-12-12 23:59:59&#39;\n=&gt; 7.0<\/code><\/pre><\/div>\n\n\n\n<p>\u3061\u3083\u3093\u3068\u6307\u5b9a\u3057\u305f\u671f\u9593\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u4e8b\u304c\u3067\u304d\u307e\u3057\u305f!<br>\u516c\u5f0f\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306b\u8a73\u3057\u304f\u66f8\u3044\u3066\u3044\u308b\u306e\u3067\u3001<a href=\"https:\/\/railsguides.jp\/active_support_core_extensions.html#time%E3%81%AE%E6%8B%A1%E5%BC%B5\">\u3053\u3061\u3089<\/a>\u3082\u5408\u308f\u305b\u3066\u8aad\u3093\u3067\u307f\u3066\u4e0b\u3055\u3044!!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7d42\u308f\u308a<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u65e5\u306frails\u3067\u30b9\u30d1\u30b2\u30c3\u30c6\u30a3\u30b3\u30fc\u30c9\u3092\u4fbf\u5229\u30e1\u30bd\u30c3\u30c9\u3067\u30ad\u30ec\u30a4\u306b\u3057\u305f\u3088\u3068\u3044\u3046\u8a71\u3067\u3059\u3002 \u74b0\u5883 \u53c2\u8003 \u3010\u77e5\u3089\u305a\u306b\u640d\u3057\u305f\u3011ActiveSupport\u306e\u671f\u9593\u6307\u5b9a\u30e1\u30bd\u30c3\u30c9all_day, all_week, all_month, al [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":567,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"swell_btn_cv_data":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[72],"class_list":["post-108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-ruby-on-rails"],"jetpack_featured_media_url":"https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2021\/10\/ActiveSupport-\u671f\u9593\u6307\u5b9a\u30e1\u30bd\u30c3\u30c9\u3067\u30b9\u30c3\u30ad\u30ea.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":3,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":2414,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/108\/revisions\/2414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/media\/567"}],"wp:attachment":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}