回答編集履歴

4

if と unless 逆に書いてた。

2019/07/30 05:23

投稿

rhiroe
rhiroe

スコア2349

test CHANGED
@@ -30,7 +30,9 @@
30
30
 
31
31
  wf = Workflow.where number: number
32
32
 
33
- return unless wf.all? { |w| Status.find_by(compensation: w.compensation progress: progress).present? }
33
+ return if wf.all? { |w| Status.find_by(compensation: w.compensation progress: progress).present? }
34
+
35
+
34
36
 
35
37
  errors[:base] << 'エラーメッセージ'
36
38
 

3

修正

2019/07/30 05:23

投稿

rhiroe
rhiroe

スコア2349

test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  included do
10
10
 
11
- after_save -> { update_progress('payment'), if: proc { self.number.present? } }
11
+ after_save -> { update_progress('payment') }, if: proc { self.number.present? }
12
12
 
13
13
  validate :check_combination_status, if: proc { self.number.present? }
14
14
 

2

修正

2019/07/30 05:15

投稿

rhiroe
rhiroe

スコア2349

test CHANGED
@@ -1,37 +1,57 @@
1
- 僕も雰囲気で書いたんですが、こんな感じですかね?
2
-
3
1
  ```rb
4
2
 
5
- def update_progress(status)
3
+ module ProgressUpdateble
6
4
 
7
- return if number.blank?
5
+ extend ActiveSupport::Concern
8
6
 
9
7
 
10
8
 
11
- Workflow.transaction do # 一応トランザクション貼ってます
9
+ included do
12
10
 
13
- wf = Workflow.where number: number
11
+ after_save -> { update_progress('payment'), if: proc { self.number.present? } }
14
12
 
15
- if wf.all? { |w| Status.exists? compensation: w.compensation, progress: progress }
13
+ validate :check_combination_status, if: proc { self.number.present? }
16
-
17
- wf.update_all progress: status
18
-
19
- else
20
-
21
- raise ActiveRecord::RecordInvalid
22
-
23
- end
24
14
 
25
15
  end
26
16
 
27
17
 
28
18
 
29
- rescue ActiveRecord::RecordInvalid
19
+ def update_progress(status)
30
20
 
31
- logger.error 'Unable to update :progress to the entered status.'
21
+ work_flows = Workflow.where(approval_status: 'pending', number: number)
32
22
 
23
+ wf.update_all progress: status
24
+
25
+ end
26
+
27
+
28
+
29
+ def check_combination_status
30
+
31
+ wf = Workflow.where number: number
32
+
33
+ return unless wf.all? { |w| Status.find_by(compensation: w.compensation progress: progress).present? }
34
+
33
- errors[:base] << 'です。'
35
+ errors[:base] << 'エラーッセージ'
36
+
37
+ end
34
38
 
35
39
  end
36
40
 
37
41
  ```
42
+
43
+ `before_save`より`after_save`の方が良さそう。
44
+
45
+ うーん、なんかもっといい方法がありそうな気もする。
46
+
47
+
48
+
49
+ `after_save`の`if:`の書き方か場所が違うかも...。
50
+
51
+ `after_save -> { ... }`の書き方がよくわからなかった、ごめんよ。
52
+
53
+
54
+
55
+ `update_all`はコールバックをスキップするのを意識しておくといいかも。
56
+
57
+ もし修正するときに`save`の無限ループに陥らないためにも。

1

修正

2019/07/30 05:13

投稿

rhiroe
rhiroe

スコア2349

test CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
  Workflow.transaction do # 一応トランザクション貼ってます
12
12
 
13
- wf = Workflow.where(number: number)
13
+ wf = Workflow.where number: number
14
14
 
15
- if wf.all? { |w| Status.find_by( compensation: w.compensation, progress: progress) }
15
+ if wf.all? { |w| Status.exists? compensation: w.compensation, progress: progress }
16
16
 
17
- wf.update_all(progress: status)
17
+ wf.update_all progress: status
18
18
 
19
19
  else
20
20