質問編集履歴

3

修正

2020/08/23 10:03

投稿

yastinbieber
yastinbieber

スコア49

test CHANGED
File without changes
test CHANGED
@@ -173,61 +173,3 @@
173
173
  Rails 5.2.4.3
174
174
 
175
175
  ruby 2.5.1
176
-
177
-
178
-
179
- ###自己解決方法
180
-
181
- ```
182
-
183
- ##idealweight.rb
184
-
185
-
186
-
187
- class Idealweight < ApplicationRecord
188
-
189
-
190
-
191
- #一部省略
192
-
193
- def self.bye_idealweight
194
-
195
- Idealweight.where("last_day<?", Date.today).destroy_all
196
-
197
- end
198
-
199
-  #終了日よりも本日の日付が大きくなった段階で該当するレコードを削除する
200
-
201
-
202
-
203
- end
204
-
205
- ```
206
-
207
- ```
208
-
209
- ##schedule.rb
210
-
211
-
212
-
213
- require File.expand_path(File.dirname(__FILE__) + "/environment")
214
-
215
- rails_env = ENV['RAILS_ENV'] || :development
216
-
217
- set :environment, rails_env
218
-
219
- set :output, "#{Rails.root}/log/cron.log"
220
-
221
-
222
-
223
- #10分毎に監視する
224
-
225
- every 10.minutes do
226
-
227
- runner "Idealweight.bye_idealweight"
228
-
229
- end
230
-
231
- ```
232
-
233
- これで無事実装することができました。

2

追記

2020/08/23 10:03

投稿

yastinbieber
yastinbieber

スコア49

test CHANGED
File without changes
test CHANGED
@@ -173,3 +173,61 @@
173
173
  Rails 5.2.4.3
174
174
 
175
175
  ruby 2.5.1
176
+
177
+
178
+
179
+ ###自己解決方法
180
+
181
+ ```
182
+
183
+ ##idealweight.rb
184
+
185
+
186
+
187
+ class Idealweight < ApplicationRecord
188
+
189
+
190
+
191
+ #一部省略
192
+
193
+ def self.bye_idealweight
194
+
195
+ Idealweight.where("last_day<?", Date.today).destroy_all
196
+
197
+ end
198
+
199
+  #終了日よりも本日の日付が大きくなった段階で該当するレコードを削除する
200
+
201
+
202
+
203
+ end
204
+
205
+ ```
206
+
207
+ ```
208
+
209
+ ##schedule.rb
210
+
211
+
212
+
213
+ require File.expand_path(File.dirname(__FILE__) + "/environment")
214
+
215
+ rails_env = ENV['RAILS_ENV'] || :development
216
+
217
+ set :environment, rails_env
218
+
219
+ set :output, "#{Rails.root}/log/cron.log"
220
+
221
+
222
+
223
+ #10分毎に監視する
224
+
225
+ every 10.minutes do
226
+
227
+ runner "Idealweight.bye_idealweight"
228
+
229
+ end
230
+
231
+ ```
232
+
233
+ これで無事実装することができました。

1

書式の改善

2020/08/23 10:02

投稿

yastinbieber
yastinbieber

スコア49

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  具体的には、
10
10
 
11
- what_day(サービスを始めて何目か)が idealweight.period(サービス提供期間)を超えた段階で idealweightテーブルのそのユーザーのレコードを自動的に削除したいです。
11
+ 本日の日付(today_date)がサービス終了(last_day_date)を超えた段階でidealweightテーブルの該当するユーザーのレコードを自動的に削除したいです。
12
12
 
13
13
 
14
14
 
@@ -22,17 +22,11 @@
22
22
 
23
23
 
24
24
 
25
+ 該当コードを下記にはらせていただきますのでご教授いただけますと幸いです。
26
+
27
+ よろしくお願い致します。
25
28
 
26
29
 
27
- ### 発生している問題・エラーメッセージ
28
-
29
-
30
-
31
- ```
32
-
33
- エラーメッセージ
34
-
35
- ```
36
30
 
37
31
 
38
32
 
@@ -56,15 +50,25 @@
56
50
 
57
51
 
58
52
 
59
- #サービス開始から何目か
53
+  #の日付
60
54
 
61
- def what_day
55
+  def today_date
62
56
 
57
+ @today = Date.today
58
+
63
- (today - idealweight.start_day).to_i+1
59
+ @today.strftime("%Y%m%d").to_i
64
60
 
65
61
  end
66
62
 
67
63
 
64
+
65
+  #サービス最終日
66
+
67
+  def last_day_date
68
+
69
+ idealweight.last_day.strftime("%Y%m%d").to_i
70
+
71
+ end
68
72
 
69
73
  end
70
74
 
@@ -82,11 +86,13 @@
82
86
 
83
87
 
84
88
 
85
- def self.delete_idealweight
89
+  #狙いは『今日の日付』が『サービス最終日』を超えた場合該当するレコードを削除します
86
90
 
87
- Idealweight.where("current_user.idealweight.period<?", current_user.what_day).destroy
91
+  def self.bye_idealweight
88
92
 
93
+ Idealweight.where("current_user.start_day_date<?", current_user.today_date).destroy_all
94
+
89
- end
95
+ end
90
96
 
91
97
 
92
98
 
@@ -110,31 +116,15 @@
110
116
 
111
117
 
112
118
 
119
+ #10分単位で監視をし、先程指定したケースで該当するレコードを削除します(idealweight.rbで指定しています)
113
120
 
121
+ every 10.minutes do
114
122
 
115
- every :day, at: "24:00" do
116
-
117
- runner "Idealweight.delete_idealweight"
123
+ runner "Idealweight.bye_idealweight"
118
124
 
119
125
  end
120
126
 
121
127
  ```
122
-
123
-
124
-
125
- 理想の実装としては、
126
-
127
- 「毎日24時にチェックを行い、サービス提供期間(idealweight.period)を本日は何日目か(what_day)が超えた場合にidealweightテーブルのそのユーザーのレコードを自動で削除したい」です。
128
-
129
-
130
-
131
- ![イメージ説明](824b00dd616ba742d643372589d645e0.png)
132
-
133
- 今朝の画面です。
134
-
135
- 18日というのがサービス提供期間(idealweight.period)で19日目というのが本日は何日目か(what_day)になります。
136
-
137
- サービスの提供期間を超えてしまっているので本来であれば本日の24時の段階でこの日付を管理しているIdealweightテーブルのレコードを削除している形です。
138
128
 
139
129
 
140
130
 
@@ -146,7 +136,7 @@
146
136
 
147
137
  ec2-user:~/environment/toreka (master) $ bundle exec whenever
148
138
 
149
- 0 0 * * * /bin/bash -l -c 'cd /home/ec2-user/environment/toreka && bundle exec bin/rails runner -e development '\''Idealweight.delete_idealweight'\'' >> /home/ec2-user/environment/toreka/log/cron.log 2>&1'
139
+ 0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /home/ec2-user/environment/toreka && bundle exec bin/rails runner -e development '\''Idealweight.bye_idealweight'\'' >> /home/ec2-user/environment/toreka/log/cron.log 2>&1'
150
140
 
151
141
 
152
142
 
@@ -168,19 +158,11 @@
168
158
 
169
159
  # End Whenever generated tasks for: /home/ec2-user/environment/toreka/config/schedule.rb at: 2020-08-18 12:18:56 +0000
170
160
 
171
- ec2-user:~/environment/toreka (master) $
161
+ ec2-user:~/environment/toreka (master) $
172
162
 
173
163
  ```
174
164
 
175
165
 
176
-
177
- いまいち見方がわからないのですがエラー自体は吐いていないようです。
178
-
179
- そのため何がうまく行っていないのかわかりません。
180
-
181
-
182
-
183
- お分かりの方是非ご教授いただけますと幸いです。
184
166
 
185
167
 
186
168