teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

修正

2020/08/23 10:03

投稿

yastinbieber
yastinbieber

スコア49

title CHANGED
File without changes
body CHANGED
@@ -85,33 +85,4 @@
85
85
  ### 補足情報(FW/ツールのバージョンなど)
86
86
 
87
87
  Rails 5.2.4.3
88
- ruby 2.5.1
88
+ ruby 2.5.1
89
-
90
- ###自己解決方法
91
- ```
92
- ##idealweight.rb
93
-
94
- class Idealweight < ApplicationRecord
95
-
96
- #一部省略
97
- def self.bye_idealweight
98
- Idealweight.where("last_day<?", Date.today).destroy_all
99
- end
100
-  #終了日よりも本日の日付が大きくなった段階で該当するレコードを削除する
101
-
102
- end
103
- ```
104
- ```
105
- ##schedule.rb
106
-
107
- require File.expand_path(File.dirname(__FILE__) + "/environment")
108
- rails_env = ENV['RAILS_ENV'] || :development
109
- set :environment, rails_env
110
- set :output, "#{Rails.root}/log/cron.log"
111
-
112
- #10分毎に監視する
113
- every 10.minutes do
114
- runner "Idealweight.bye_idealweight"
115
- end
116
- ```
117
- これで無事実装することができました。

2

追記

2020/08/23 10:03

投稿

yastinbieber
yastinbieber

スコア49

title CHANGED
File without changes
body CHANGED
@@ -85,4 +85,33 @@
85
85
  ### 補足情報(FW/ツールのバージョンなど)
86
86
 
87
87
  Rails 5.2.4.3
88
- ruby 2.5.1
88
+ ruby 2.5.1
89
+
90
+ ###自己解決方法
91
+ ```
92
+ ##idealweight.rb
93
+
94
+ class Idealweight < ApplicationRecord
95
+
96
+ #一部省略
97
+ def self.bye_idealweight
98
+ Idealweight.where("last_day<?", Date.today).destroy_all
99
+ end
100
+  #終了日よりも本日の日付が大きくなった段階で該当するレコードを削除する
101
+
102
+ end
103
+ ```
104
+ ```
105
+ ##schedule.rb
106
+
107
+ require File.expand_path(File.dirname(__FILE__) + "/environment")
108
+ rails_env = ENV['RAILS_ENV'] || :development
109
+ set :environment, rails_env
110
+ set :output, "#{Rails.root}/log/cron.log"
111
+
112
+ #10分毎に監視する
113
+ every 10.minutes do
114
+ runner "Idealweight.bye_idealweight"
115
+ end
116
+ ```
117
+ これで無事実装することができました。

1

書式の改善

2020/08/23 10:02

投稿

yastinbieber
yastinbieber

スコア49

title CHANGED
File without changes
body CHANGED
@@ -3,20 +3,17 @@
3
3
  あるアクションが起きた際にそのユーザーのあるレコードを自動で削除したいと考えております。
4
4
 
5
5
  具体的には、
6
- what_day(サービスを始めて何目か)が idealweight.period(サービス提供期間)を超えた段階で idealweightテーブルのそのユーザーのレコードを自動的に削除したいです。
6
+ の日付(today_date)がサービス終了日(last_day_date)を超えた段階でidealweightテーブルの該当するユーザーのレコードを自動的に削除したいです。
7
7
 
8
8
  ちなみにidealweightモデルは、
9
9
  userモデルと1:1の関係性を持っています。
10
10
 
11
11
  [前回の質問](https://teratail.com/questions/285275#reply-405221)でcronを用いるとお教えいただいたので実装を試みたのですが実行された様子がなく困っております。
12
12
 
13
+ 該当コードを下記にはらせていただきますのでご教授いただけますと幸いです。
14
+ よろしくお願い致します。
13
15
 
14
- ### 発生している問題・エラーメッセージ
15
16
 
16
- ```
17
- エラーメッセージ
18
- ```
19
-
20
17
  ### 該当のソースコード
21
18
 
22
19
  ```ここに言語名を入力
@@ -27,11 +24,16 @@
27
24
 
28
25
  has_one :idealweight, dependent: :destroy
29
26
 
30
- #サービス開始から何目か
27
+  #の日付
31
- def what_day
28
+  def today_date
29
+ @today = Date.today
32
- (today - idealweight.start_day).to_i+1
30
+ @today.strftime("%Y%m%d").to_i
33
31
  end
34
32
 
33
+  #サービス最終日
34
+  def last_day_date
35
+ idealweight.last_day.strftime("%Y%m%d").to_i
36
+ end
35
37
  end
36
38
 
37
39
  ```
@@ -40,9 +42,10 @@
40
42
  class Idealweight < ApplicationRecord
41
43
   belongs_to :user
42
44
 
45
+  #狙いは『今日の日付』が『サービス最終日』を超えた場合該当するレコードを削除します
43
- def self.delete_idealweight
46
+  def self.bye_idealweight
44
- Idealweight.where("current_user.idealweight.period<?", current_user.what_day).destroy
47
+ Idealweight.where("current_user.start_day_date<?", current_user.today_date).destroy_all
45
- end
48
+ end
46
49
 
47
50
  end
48
51
  ```
@@ -54,25 +57,17 @@
54
57
  set :environment, rails_env
55
58
  set :output, "#{Rails.root}/log/cron.log"
56
59
 
57
-
60
+ #10分単位で監視をし、先程指定したケースで該当するレコードを削除します(idealweight.rbで指定しています)
58
- every :day, at: "24:00" do
61
+ every 10.minutes do
59
- runner "Idealweight.delete_idealweight"
62
+ runner "Idealweight.bye_idealweight"
60
63
  end
61
64
  ```
62
65
 
63
- 理想の実装としては、
64
- 「毎日24時にチェックを行い、サービス提供期間(idealweight.period)を本日は何日目か(what_day)が超えた場合にidealweightテーブルのそのユーザーのレコードを自動で削除したい」です。
65
-
66
- ![イメージ説明](824b00dd616ba742d643372589d645e0.png)
67
- 今朝の画面です。
68
- 18日というのがサービス提供期間(idealweight.period)で19日目というのが本日は何日目か(what_day)になります。
69
- サービスの提供期間を超えてしまっているので本来であれば本日の24時の段階でこの日付を管理しているIdealweightテーブルのレコードを削除している形です。
70
-
71
66
  ### 試したこと
72
67
 
73
68
  ```
74
69
  ec2-user:~/environment/toreka (master) $ bundle exec whenever
75
- 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'
70
+ 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'
76
71
 
77
72
  ## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
78
73
  ## [message] Run `whenever --help' for more options.
@@ -83,14 +78,10 @@
83
78
  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'
84
79
 
85
80
  # End Whenever generated tasks for: /home/ec2-user/environment/toreka/config/schedule.rb at: 2020-08-18 12:18:56 +0000
86
- ec2-user:~/environment/toreka (master) $
81
+ ec2-user:~/environment/toreka (master) $
87
82
  ```
88
83
 
89
- いまいち見方がわからないのですがエラー自体は吐いていないようです。
90
- そのため何がうまく行っていないのかわかりません。
91
84
 
92
- お分かりの方是非ご教授いただけますと幸いです。
93
-
94
85
  ### 補足情報(FW/ツールのバージョンなど)
95
86
 
96
87
  Rails 5.2.4.3