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