質問編集履歴

3

追記

2016/10/26 08:30

投稿

gogoackman3
gogoackman3

スコア109

test CHANGED
File without changes
test CHANGED
@@ -271,3 +271,65 @@
271
271
 
272
272
 
273
273
  ご教示頂ければ幸いです。
274
+
275
+
276
+
277
+ ###追記
278
+
279
+
280
+
281
+ 以下の方法で①の問題は解決出来ました。
282
+
283
+
284
+
285
+ ▼user.rb に以下のfollowing_gamesを追加
286
+
287
+
288
+
289
+ ```ruby
290
+
291
+ has_many :follow_games
292
+
293
+ has_many :following_games, through: :follow_games, source: :game
294
+
295
+ ```
296
+
297
+
298
+
299
+ ▼follow_games_controller.rb もfollowing_gamesに修正
300
+
301
+
302
+
303
+ ```ruby
304
+
305
+ def create
306
+
307
+ current_user.following_games << @game unless current_user.following_games.exists?(@game.id)
308
+
309
+ end
310
+
311
+ ```
312
+
313
+
314
+
315
+ ###しかし!
316
+
317
+
318
+
319
+ 今度は②の部分でinsertしようとすると、以下のエラーが発生しているようです。
320
+
321
+
322
+
323
+ ```ここに言語を入力
324
+
325
+ NoMethodError in FollowGamesController#create
326
+
327
+ undefined method `rank' for #<Game:0x007f2278169e18>
328
+
329
+ ```
330
+
331
+
332
+
333
+ rankはfollow_gamesテーブルのカラムなので、gameテーブルにはありません。
334
+
335
+ どうにしかして、postで受け取ったrank情報もfollow_gamesテーブルに追加する方法はないでしょうか???

2

修正

2016/10/26 08:30

投稿

gogoackman3
gogoackman3

スコア109

test CHANGED
File without changes
test CHANGED
@@ -46,6 +46,56 @@
46
46
 
47
47
  ```ruby
48
48
 
49
+ has_many :follow_games
50
+
51
+ ```
52
+
53
+
54
+
55
+ ②のテーブル
56
+
57
+ ```ruby
58
+
59
+ class CreateGames < ActiveRecord::Migration[5.0]
60
+
61
+ def change
62
+
63
+ create_table :games do |t|
64
+
65
+ t.boolean :active, null: false, default: true
66
+
67
+ t.string :title, null: false, default: ''
68
+
69
+ t.string :icon, null: false, default: ''
70
+
71
+ t.string :rank_name, null: false, default: ''
72
+
73
+ t.string :search_tag, null: false, default: ''
74
+
75
+ t.integer :feed_range, null: false, default: 0
76
+
77
+ t.integer :follower_num, null: false, default: 0
78
+
79
+
80
+
81
+ t.timestamps
82
+
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ ```
90
+
91
+
92
+
93
+ ③follow_game.rb
94
+
95
+
96
+
97
+ ```ruby
98
+
49
99
  belongs_to :user
50
100
 
51
101
  belongs_to :game
@@ -54,29 +104,25 @@
54
104
 
55
105
 
56
106
 
57
- のテーブル
107
+ のテーブル
58
-
108
+
109
+
110
+
59
- ```ruby
111
+ ```ruby
60
-
112
+
61
- class CreateGames < ActiveRecord::Migration[5.0]
113
+ class CreateFollowGames < ActiveRecord::Migration[5.0]
62
114
 
63
115
  def change
64
116
 
65
- create_table :games do |t|
117
+ create_table :follow_games do |t|
66
-
118
+
67
- t.boolean :active, null: false, default: true
119
+ t.boolean :active, null: false, default: true
68
-
69
- t.string :title, null: false, default: ''
120
+
70
-
71
- t.string :icon, null: false, default: ''
72
-
73
- t.string :rank_name, null: false, default: ''
74
-
75
- t.string :search_tag, null: false, default: ''
76
-
77
- t.integer :feed_range, null: false, default: 0
121
+ t.references :user, foreign_key: true
78
-
122
+
79
- t.integer :follower_num, null: false, default: 0
123
+ t.references :game, foreign_key: true
124
+
125
+ t.integer :rank
80
126
 
81
127
 
82
128
 
@@ -92,130 +138,84 @@
92
138
 
93
139
 
94
140
 
141
+ しかし、これだと、2点問題が発生しています。
142
+
143
+
144
+
145
+ **問題その1**
146
+
147
+
148
+
149
+ ユーザーがゲームをフォローする際に、既にフォローしているか判定する処理を入れており、その時のSQL文が誤ってしまう。
150
+
151
+
152
+
95
- ③follow_game.rb
153
+ ▼実際のフォローするフォーム
96
-
97
-
98
-
154
+
99
- ```ruby
155
+ ```ruby
100
-
156
+
101
- has_many :follow_games
157
+ = form_for(current_user.follow_games.build) do |f|
158
+
102
-
159
+ = f.text_field :rank, id:'rank', placeholder: "#{game.rank_name}を入力"
160
+
161
+ = f.hidden_field :game_id, value:"#{game.id}"
162
+
163
+ = f.hidden_field :user_id, value:"#{current_user.id}"
164
+
165
+
166
+
167
+ = f.submit '追加', id:"rank_set"
168
+
103
- ```
169
+ ```
104
-
105
-
106
-
170
+
171
+
172
+
107
- ③のテーブル
173
+ ▼follow_game.controller.rb
108
-
109
-
110
-
174
+
175
+
176
+
111
- ```ruby
177
+ ```ruby
112
-
178
+
113
- class CreateFollowGames < ActiveRecord::Migration[5.0]
179
+ class FollowGamesController < ApplicationController
180
+
181
+
182
+
114
-
183
+ before_action :set_game
184
+
185
+
186
+
115
- def change
187
+ def create
188
+
116
-
189
+ current_user.follow_games << @game unless current_user.follow_games.exists?(@game.id)
190
+
191
+ end
192
+
193
+
194
+
195
+ def delete
196
+
117
- create_table :follow_games do |t|
197
+ current_user.follow_games.destroy(@game)
118
-
119
- t.boolean :active, null: false, default: true
198
+
120
-
121
- t.references :user, foreign_key: true
122
-
123
- t.references :game, foreign_key: true
124
-
125
- t.integer :rank
199
+ @game.reload
200
+
126
-
201
+ end
202
+
203
+
204
+
127
-
205
+ private
128
-
206
+
129
- t.timestamps
207
+ def set_game
208
+
209
+ @game = Game.find(params[:follow_game][:game_id])
130
210
 
131
211
  end
132
212
 
133
- end
134
-
135
213
  end
136
214
 
137
215
  ```
138
216
 
139
217
 
140
218
 
141
- しかし、これだと、2点問題が発生しています。
142
-
143
-
144
-
145
- **問題その1**
146
-
147
-
148
-
149
- ユーザーがゲームをフォローする際に、既にフォローしているか判定する処理を入れており、その時のSQL文が誤ってしまう。
150
-
151
-
152
-
153
- ▼実際のフォローするフォーム
154
-
155
- ```ruby
156
-
157
- = form_for(current_user.follow_games.build) do |f|
158
-
159
- = f.text_field :rank, id:'rank', placeholder: "#{game.rank_name}を入力"
160
-
161
- = f.hidden_field :game_id, value:"#{game.id}"
162
-
163
- = f.hidden_field :user_id, value:"#{current_user.id}"
164
-
165
-
166
-
167
- = f.submit '追加', id:"rank_set"
168
-
169
- ```
170
-
171
-
172
-
173
- ▼follow_game.controller.rb
174
-
175
-
176
-
177
- ```ruby
178
-
179
- class FollowGamesController < ApplicationController
180
-
181
-
182
-
183
- before_action :set_game
184
-
185
-
186
-
187
- def create
188
-
189
- current_user.follow_games << @game unless current_user.follow_games.exists?(@game.id)
190
-
191
- end
192
-
193
-
194
-
195
- def delete
196
-
197
- current_user.follow_games.destroy(@game)
198
-
199
- @game.reload
200
-
201
- end
202
-
203
-
204
-
205
- private
206
-
207
- def set_game
208
-
209
- @game = Game.find(params[:follow_game][:game_id])
210
-
211
- end
212
-
213
- end
214
-
215
- ```
216
-
217
-
218
-
219
219
  ▼フォームからPOSTした際の重複確認のログ
220
220
 
221
221
  ```ここに言語を入力

1

誤字の修正

2016/10/26 06:57

投稿

gogoackman3
gogoackman3

スコア109

test CHANGED
File without changes
test CHANGED
@@ -142,7 +142,11 @@
142
142
 
143
143
 
144
144
 
145
+ **問題その1**
146
+
147
+
148
+
145
- ユーザーがゲームをフォローする際に、既にフォローしているか判定する処理を入れており、その時のSQL文が誤ってしまう。
149
+ ユーザーがゲームをフォローする際に、既にフォローしているか判定する処理を入れており、その時のSQL文が誤ってしまう。
146
150
 
147
151
 
148
152
 
@@ -182,7 +186,7 @@
182
186
 
183
187
  def create
184
188
 
185
- current_user.follow_games << @game.id unless current_user.follow_games.exists?(@game.id)
189
+ current_user.follow_games << @game unless current_user.follow_games.exists?(@game.id)
186
190
 
187
191
  end
188
192
 
@@ -248,6 +252,8 @@
248
252
 
249
253
 
250
254
 
255
+ **問題その2**
256
+
251
257
  ②偶然にも重複しなく、insertが走った場合でも、以下のエラーが発生する。
252
258
 
253
259