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

回答編集履歴

4

if と unless 逆に書いてた。

2019/07/30 05:23

投稿

rhiroe
rhiroe

スコア2352

answer CHANGED
@@ -14,7 +14,8 @@
14
14
 
15
15
  def check_combination_status
16
16
  wf = Workflow.where number: number
17
- return unless wf.all? { |w| Status.find_by(compensation: w.compensation progress: progress).present? }
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

修正

2019/07/30 05:23

投稿

rhiroe
rhiroe

スコア2352

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

修正

2019/07/30 05:15

投稿

rhiroe
rhiroe

スコア2352

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

1

修正

2019/07/30 05:13

投稿

rhiroe
rhiroe

スコア2352

answer CHANGED
@@ -4,9 +4,9 @@
4
4
  return if number.blank?
5
5
 
6
6
  Workflow.transaction do # 一応トランザクション貼ってます
7
- wf = Workflow.where(number: number)
7
+ wf = Workflow.where number: number
8
- if wf.all? { |w| Status.find_by( compensation: w.compensation, progress: progress) }
8
+ if wf.all? { |w| Status.exists? compensation: w.compensation, progress: progress }
9
- wf.update_all(progress: status)
9
+ wf.update_all progress: status
10
10
  else
11
11
  raise ActiveRecord::RecordInvalid
12
12
  end