回答編集履歴

7

ミス修正

2016/09/28 18:56

投稿

退会済みユーザー
test CHANGED
@@ -160,7 +160,7 @@
160
160
 
161
161
  current_month = current_month.months_ago(back)
162
162
 
163
- range = current_month.months_ago(back)...current_month.next_month
163
+ range = current_month...current_month.next_month
164
164
 
165
165
  User.where(user_id: @user.id, created_at:range).count
166
166
 

6

コードのミスを修正

2016/09/28 18:56

投稿

退会済みユーザー
test CHANGED
@@ -120,7 +120,9 @@
120
120
 
121
121
  (0..back_months).map do |back|
122
122
 
123
+ current_month = current_month.months_ago(back)
124
+
123
- range = current_month.months_ago(back)...current_month.next_month
125
+ range = current_month...current_month.next_month
124
126
 
125
127
  User.where(user_id: @user.id, created_at:range).count
126
128
 
@@ -156,6 +158,8 @@
156
158
 
157
159
  (0..back_months).map do |back|
158
160
 
161
+ current_month = current_month.months_ago(back)
162
+
159
163
  range = current_month.months_ago(back)...current_month.next_month
160
164
 
161
165
  User.where(user_id: @user.id, created_at:range).count

5

誤字修正

2016/09/28 18:49

投稿

退会済みユーザー
test CHANGED
@@ -152,7 +152,7 @@
152
152
 
153
153
  def fetch_during_month_data(start_month=Date.today.months_ago(2),end_month=Date.today)
154
154
 
155
- back_months = (end_month.year - start_month.year)*12 - a.month + b.month
155
+ back_months = (end_month.year - start_month.year)*12 - start_month.month + end_month.month
156
156
 
157
157
  (0..back_months).map do |back|
158
158
 

4

間違いを修正

2016/09/28 18:37

投稿

退会済みユーザー
test CHANGED
@@ -150,7 +150,7 @@
150
150
 
151
151
 
152
152
 
153
- def fetch_during_month_data(start_month=Date.today.month-2,end_month=Date.today.month)
153
+ def fetch_during_month_data(start_month=Date.today.months_ago(2),end_month=Date.today)
154
154
 
155
155
  back_months = (end_month.year - start_month.year)*12 - a.month + b.month
156
156
 
@@ -170,6 +170,6 @@
170
170
 
171
171
 
172
172
 
173
- でできます。
173
+ とすることでできます。
174
174
 
175
175
 

3

さらに良い回答に修正

2016/09/28 18:34

投稿

退会済みユーザー
test CHANGED
@@ -99,3 +99,77 @@
99
99
  ```
100
100
 
101
101
  とします。
102
+
103
+
104
+
105
+ 追記
106
+
107
+
108
+
109
+ 上記コードは,年次ごとにはわかりやすいのですが,年をまたげないので年をまたぐ必要がある場合には`fetch_during_month_data`を以下のように修正します。
110
+
111
+
112
+
113
+
114
+
115
+ ```ruby
116
+
117
+ def fetch_during_month_data(end_month=Date.today.month,back_months = 2 ,year=Date.today.year)
118
+
119
+ current_month = Date.new(year,end_month)
120
+
121
+ (0..back_months).map do |back|
122
+
123
+ range = current_month.months_ago(back)...current_month.next_month
124
+
125
+ User.where(user_id: @user.id, created_at:range).count
126
+
127
+ end
128
+
129
+ end
130
+
131
+ ```
132
+
133
+
134
+
135
+ 期間指定したい場合は,
136
+
137
+
138
+
139
+ ```ruby
140
+
141
+ def main_action
142
+
143
+ @this_month,@last_month,@before_last_month = fetch_during_month_data
144
+
145
+ end
146
+
147
+
148
+
149
+ private
150
+
151
+
152
+
153
+ def fetch_during_month_data(start_month=Date.today.month-2,end_month=Date.today.month)
154
+
155
+ back_months = (end_month.year - start_month.year)*12 - a.month + b.month
156
+
157
+ (0..back_months).map do |back|
158
+
159
+ range = current_month.months_ago(back)...current_month.next_month
160
+
161
+ User.where(user_id: @user.id, created_at:range).count
162
+
163
+ end
164
+
165
+ end
166
+
167
+
168
+
169
+ ```
170
+
171
+
172
+
173
+ でできます。
174
+
175
+

2

補足

2016/09/28 18:32

投稿

退会済みユーザー
test CHANGED
@@ -57,3 +57,45 @@
57
57
  end
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ もしくはfetch_monthly_data(range)はまとめてしまって
64
+
65
+
66
+
67
+ ```ruby
68
+
69
+
70
+
71
+ def main_action
72
+
73
+ @this_month,@last_month,@before_last_month = fetch_during_month_data
74
+
75
+ end
76
+
77
+
78
+
79
+ private
80
+
81
+
82
+
83
+ def fetch_during_month_data(start_month=Date.today.month-2,end_month=Date.today.month,year=Date.today.year)
84
+
85
+ end_month.downto(start_month).map do |target_month|
86
+
87
+ current_month = Date.new(year,target_month)
88
+
89
+ range = current_month...current_month.next_month
90
+
91
+ User.where(user_id: @user.id, created_at:range).count
92
+
93
+ end
94
+
95
+ end
96
+
97
+
98
+
99
+ ```
100
+
101
+ とします。

1

情報修正

2016/09/28 17:38

投稿

退会済みユーザー
test CHANGED
@@ -14,9 +14,9 @@
14
14
 
15
15
  を考え,実際にサーバにどのようなqueryが投げられているか確認すると,無駄を削減できると思います。
16
16
 
17
- また,共通の処理を関数に切り出し,いいかもしれません。私は以下のように特定の月次ではなく汎用性を高くします。
17
+ また,共通の処理を関数に切り出し,いいかもしれません。私は以下のように特定の月次ではなく任意の区間を指定できるような実装にして汎用性を高くします。
18
18
 
19
- もし月次の定義がずれていた場合は,適宣保管していただけると幸いです。
19
+ もし月次の定義がずれていた場合は,適宣補完していただけると幸いです。
20
20
 
21
21
 
22
22