質問編集履歴
5
入力画面のコードを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
###前提・実現したいこと
|
2
2
|
|
3
|
-
ruby on railsで、取引アカウント同士の取引記録を実装してい
|
3
|
+
ruby on railsで、取引アカウント同士の取引記録を実装しています。
|
4
|
+
|
5
|
+
|
6
|
+
|
4
|
-
|
7
|
+
入力フォームで、ユーザーが「取引相手の口座番号」と「取引額」の2項目を入力するだけで、
|
5
|
-
|
6
8
|
|
7
9
|
**取引モデル(親モデル)で1件のレコードが作成されると同時に、
|
8
10
|
|
@@ -88,6 +90,58 @@
|
|
88
90
|
|
89
91
|
```
|
90
92
|
|
93
|
+
取引入力画面です。
|
94
|
+
|
95
|
+
app/views/account_transactions.html.erb
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
```ここに言語を入力
|
100
|
+
|
101
|
+
<%= form_for @account_transaction do |f|%>
|
102
|
+
|
103
|
+
<% if @account_transaction.errors.any? %>
|
104
|
+
|
105
|
+
<h3>入力内容にエラーが<%= @account_transaction.errors.count %>件あります</h3>
|
106
|
+
|
107
|
+
<ul>
|
108
|
+
|
109
|
+
<% @account_transaction.errors.full_messages.each do |message| %>
|
110
|
+
|
111
|
+
<li><%= message %></li>
|
112
|
+
|
113
|
+
<% end %>
|
114
|
+
|
115
|
+
</ul>
|
116
|
+
|
117
|
+
<% end %>
|
118
|
+
|
119
|
+
<div class="field">
|
120
|
+
|
121
|
+
<%= f.label :送金相手の口座番号 %>
|
122
|
+
|
123
|
+
<%= f.text_field :deposit_account_id %>
|
124
|
+
|
125
|
+
<%# <%= f.number_field :deposit_account_id %>
|
126
|
+
|
127
|
+
<%= f.label :送金額 %>
|
128
|
+
|
129
|
+
<%= f.number_field :amount %>
|
130
|
+
|
131
|
+
<%= f.submit"送金確認" %>
|
132
|
+
|
133
|
+
</div>
|
134
|
+
|
135
|
+
<div>
|
136
|
+
|
137
|
+
<%= @account_transaction.errors.to_yaml %>
|
138
|
+
|
139
|
+
</div>
|
140
|
+
|
141
|
+
<% end %>
|
142
|
+
|
143
|
+
```
|
144
|
+
|
91
145
|
|
92
146
|
|
93
147
|
AccountTransactionのコントローラには、取引モデルへの記録と同時に、
|
4
質問文の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
**取引モデル(親モデル)で1件のレコードが作成されると同時に、
|
8
8
|
|
9
|
-
1:多として関連付けを施してある入出金モデル(子モデル)に複数のレコードを作成したい**と思っています。
|
9
|
+
1:多として関連付けを施してある入出金モデル(子モデル)に複数のレコードを自動的に作成したい**と思っています。
|
10
10
|
|
11
11
|
作成するレコードは、
|
12
12
|
|
3
アドバイスを参考に修正コードを記述
test
CHANGED
File without changes
|
test
CHANGED
@@ -199,3 +199,97 @@
|
|
199
199
|
Rails 5.1.3
|
200
200
|
|
201
201
|
ruby 2.4.1
|
202
|
+
|
203
|
+
###追記:アドバイスを受けて修正したコード
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
AccountTransactionのコントローラファイル。
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
|
211
|
+
class AccountTransactionsController < ApplicationController
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
#取引を作成する
|
218
|
+
|
219
|
+
def new
|
220
|
+
|
221
|
+
@account_transaction = AccountTransaction.new
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
#取引を作成して記録する
|
228
|
+
|
229
|
+
def create
|
230
|
+
|
231
|
+
@account_transaction = AccountTransaction.new(
|
232
|
+
|
233
|
+
withdrawal_account_id: current_user.basic_income_account.id,
|
234
|
+
|
235
|
+
deposit_account_id: BasicIncomeAccount.find_by(account_number: params[:account_transaction][:deposit_account_id]).id,
|
236
|
+
|
237
|
+
amount: params[:account_transaction][:amount]
|
238
|
+
|
239
|
+
)
|
240
|
+
|
241
|
+
#####################修正中箇所#############################
|
242
|
+
|
243
|
+
#出金側
|
244
|
+
|
245
|
+
@deposits_and_withdrawal1 = DepositsAndWithdrawal.new
|
246
|
+
|
247
|
+
@deposits_and_withdrawal1.transaction_type = '出金'
|
248
|
+
|
249
|
+
@deposits_and_withdrawal1.basic_income_account_id = current_user.basic_income_account.id
|
250
|
+
|
251
|
+
@deposits_and_withdrawal1.amount = -1 * params[:account_transaction][:amount].to_f
|
252
|
+
|
253
|
+
@account_transaction.deposits_and_withdrawals << @deposits_and_withdrawal1
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
#入金側
|
258
|
+
|
259
|
+
@deposits_and_withdrawal2 = DepositsAndWithdrawal.new
|
260
|
+
|
261
|
+
@deposits_and_withdrawal2.transaction_type = '出金'
|
262
|
+
|
263
|
+
@deposits_and_withdrawal2.basic_income_account_id = BasicIncomeAccount.find_by(account_number: params[:account_transaction][:deposit_account_id]).id,
|
264
|
+
|
265
|
+
@deposits_and_withdrawal2.amount = params[:account_transaction][:amount]
|
266
|
+
|
267
|
+
@account_transaction.deposits_and_withdrawals << @deposits_and_withdrawal2
|
268
|
+
|
269
|
+
#################修正中箇所はここまで######################
|
270
|
+
|
271
|
+
###@account_transactionを保存し、保存できたらホーム画面(root_path)へ、失敗なら取引入力画面(new)へ飛ぶ。
|
272
|
+
|
273
|
+
if @account_transaction.save
|
274
|
+
|
275
|
+
redirect_to root_path
|
276
|
+
|
277
|
+
else
|
278
|
+
|
279
|
+
render "new"
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
def account_transaction_params
|
288
|
+
|
289
|
+
params.require(:account_transaction).permit(:deposit_account_id, :amount)
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
```
|
2
文法の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,9 +32,7 @@
|
|
32
32
|
|
33
33
|
2つ目は、入出金モデルの出金レコードの取引カラムの数値をマイナス数値として記録されないことに、
|
34
34
|
|
35
|
-
困っています。
|
35
|
+
困っています。(入力フォームから渡されたパラメータの数値をマイナス数値に変換する方法がわからなくて困っています)
|
36
|
-
|
37
|
-
どうすればいいでしょうか?
|
38
36
|
|
39
37
|
|
40
38
|
|
1
質問文の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
###前提・実現したいこと
|
2
2
|
|
3
|
-
ruby on railsで、取引アカウント同士の
|
3
|
+
ruby on railsで、取引アカウント同士の取引記録を実装していて、
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
-
取引モデル(親モデル)で1件のレコードが作成されると同時に、
|
7
|
+
**取引モデル(親モデル)で1件のレコードが作成されると同時に、
|
8
|
-
|
8
|
+
|
9
|
-
関連付けを施してある入出金モデル(子モデル)に複数のレコードを作成したいと思っています。
|
9
|
+
1:多として関連付けを施してある入出金モデル(子モデル)に複数のレコードを作成したい**と思っています。
|
10
10
|
|
11
11
|
作成するレコードは、
|
12
12
|
|
@@ -26,19 +26,19 @@
|
|
26
26
|
|
27
27
|
undefined method `transaction_type=' for #<DepositsAndWithdrawal::ActiveRecord_Associations_CollectionProxy:0x007f189cc329c0>
|
28
28
|
|
29
|
-
というエラーが発生してしまってい
|
29
|
+
というエラーが発生してしまって困っています。
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
-
2つ目は、入出金モデルの取引
|
33
|
+
2つ目は、入出金モデルの出金レコードの取引カラムの数値をマイナス数値として記録されないことに、
|
34
|
-
|
34
|
+
|
35
|
-
入
|
35
|
+
困っています。言い換えるならば、入力フォームから渡された正の数値をマイナスの数値に換えるには
|
36
|
-
|
36
|
+
|
37
|
-
|
37
|
+
どうすればいいでしょうか?
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
-
どうすれば、この問題を解消できるか教えて頂ければ幸いです。よろしくお願いします。
|
41
|
+
どうすれば、これらの問題を解消できるか教えて頂ければ幸いです。よろしくお願いします。
|
42
42
|
|
43
43
|
|
44
44
|
|
@@ -50,7 +50,7 @@
|
|
50
50
|
|
51
51
|
1:多の関連付けを施してあります。
|
52
52
|
|
53
|
-
```
|
53
|
+
```ruby
|
54
54
|
|
55
55
|
class AccountTransaction < ApplicationRecord
|
56
56
|
|
@@ -70,7 +70,7 @@
|
|
70
70
|
|
71
71
|
```
|
72
72
|
|
73
|
-
```
|
73
|
+
```ruby
|
74
74
|
|
75
75
|
class DepositsAndWithdrawal < ApplicationRecord
|
76
76
|
|
@@ -94,19 +94,21 @@
|
|
94
94
|
|
95
95
|
AccountTransactionのコントローラには、取引モデルへの記録と同時に、
|
96
96
|
|
97
|
-
入出金モデルにもレコードを作成するように以下のように記述をしました
|
97
|
+
入出金モデルにもレコードを作成するようにbuildメソッドを使い以下のように記述をしました。
|
98
|
+
|
98
|
-
|
99
|
+
しかし、
|
100
|
+
|
99
|
-
|
101
|
+
入力フォームで、取引を入力してsubmitボタンを押すと、
|
100
102
|
|
101
103
|
NoMethodError in AccountTransactionsController#create
|
102
104
|
|
103
105
|
undefined method `transaction_type=' for #<DepositsAndWithdrawal::ActiveRecord_Associations_CollectionProxy:0x007f189cc329c0>
|
104
106
|
|
105
|
-
|
106
|
-
|
107
|
-
というエラー
|
107
|
+
というエラーになってしまいます。
|
108
|
-
|
108
|
+
|
109
|
+
|
110
|
+
|
109
|
-
```
|
111
|
+
```ruby
|
110
112
|
|
111
113
|
class AccountTransactionsController < ApplicationController
|
112
114
|
|
@@ -136,7 +138,7 @@
|
|
136
138
|
|
137
139
|
@account_transaction.deposits_and_withdrawals.basic_income_account_id = current_user.basic_income_account.id
|
138
140
|
|
139
|
-
@account_transaction.deposits_and_withdrawals.amount = -params[:account_transaction][:amount]
|
141
|
+
@account_transaction.deposits_and_withdrawals.amount = -params[:account_transaction][:amount]#ここの渡された数値をマイナス数値にしたい
|
140
142
|
|
141
143
|
@account_transaction.save
|
142
144
|
|
@@ -180,6 +182,12 @@
|
|
180
182
|
|
181
183
|
```
|
182
184
|
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
183
191
|
ここまで読んでいただきありがとうございました。
|
184
192
|
|
185
193
|
もし、足りない部分を指摘して頂ければ追記します。
|