質問編集履歴

8

追記 part3

2019/12/14 12:37

投稿

clora
clora

スコア72

test CHANGED
File without changes
test CHANGED
@@ -479,3 +479,29 @@
479
479
  ただ、まだ検索結果のビューでは、@production_resulはnilclassになってしまいますね、、、。
480
480
 
481
481
  うまく変数が渡せていないだけだと思うのですが、、、。
482
+
483
+
484
+
485
+ ### 追記 part3
486
+
487
+ なんとか解決しました。
488
+
489
+ ビューの方の表記を変えたところきちんと検索結果が表示されました。
490
+
491
+ ```html
492
+
493
+ 間違い
494
+
495
+ <%= @production_result.date %>
496
+
497
+
498
+
499
+ 正解
500
+
501
+ <%= (@production_result.to_a)[0].date %>
502
+
503
+ ```
504
+
505
+
506
+
507
+ また、asmさんのアドバイス通りwhereでなく、find_byを使えば@production_result.dateという形でも取り出せます。whereやfind_byの違いや原理を理解していないのがエラーにハマった原因でした。

7

誤字の修正

2019/12/14 12:37

投稿

clora
clora

スコア72

test CHANGED
File without changes
test CHANGED
@@ -442,42 +442,34 @@
442
442
 
443
443
  @production_resultがnilになってしまうのはproduct_idの指定の仕方が間違えていたことによるものでした。
444
444
 
445
- 上の方にも書いた通り、product_idは他のテーブルに値を探しにいって変数product_idに入れて、searchメソッドに渡しています。その時にフォームから直接受け取ったgroup_idは"1"となっていたのに対し、product_idは 1 となっていました。そのため、そのまま、product_idを検索条件に入れてしまうと、条件に引っかからず、検索結果が0となり、```ruby
445
+ 上の方にも書いた通り、product_idは他のテーブルに値を探しにいって変数product_idに入れて、searchメソッドに渡しています。その時にフォームから直接受け取ったgroup_idは"1"となっていたのに対し、product_idは 1 となっていました。そのため、そのまま、product_idを検索条件に入れてしまうと、条件に引っかからず、検索結果が0となり、@production_resulがnilになっているのでしたそこで下記のように、searchメソッドを書き直したところ、きちんと検索結果が、@production_resultに入ってくれました。
446
+
447
+
448
+
449
+ ```ruby
450
+
451
+ #production.rb
452
+
453
+ def self.search(date,line_id,product_id)
454
+
455
+ Production.where(date: date).
456
+
457
+ where(group_id: group_id).
458
+
459
+ where(product_id: "#{product_id}")
460
+
461
+ end
462
+
463
+ ```
464
+
465
+ ```ruby
446
466
 
447
467
  [1] pry(#<Productions::SearchesController>)> product_id
448
468
 
449
- => 1
469
+ => 1 #こっちはただの数字
450
470
 
451
471
  [2] pry(#<Productions::SearchesController>)> params[:production][:group_id]
452
472
 
453
- => "1"がnilになっているのでしたそこで下記のように、searchメソッドを書き直したところ、きちんと検索結果が、@production_resultに入ってくれました。
454
-
455
-
456
-
457
- ```ruby
458
-
459
- #production.rb
460
-
461
- def self.search(date,line_id,product_id)
462
-
463
- Production.where(date: date).
464
-
465
- where(group_id: group_id).
466
-
467
- where(product_id: "#{product_id}")
468
-
469
- end
470
-
471
- ```
472
-
473
- ```ruby
474
-
475
- [1] pry(#<Productions::SearchesController>)> product_id
476
-
477
- => 1 #こっちはただの数字
478
-
479
- [2] pry(#<Productions::SearchesController>)> params[:production][:group_id]
480
-
481
473
  => "1" #こっちはダブルクォーテーションで囲まれている
482
474
 
483
475
  ```

6

追記 part2

2019/12/14 11:51

投稿

clora
clora

スコア72

test CHANGED
File without changes
test CHANGED
@@ -433,3 +433,57 @@
433
433
  ### 試したこと
434
434
 
435
435
  binding pryをしてみたところ、seaechメソッドをすると@production_resultが[]となってしまいます。
436
+
437
+
438
+
439
+ ### 追記 part2
440
+
441
+ 上記の状況から進展がありました。
442
+
443
+ @production_resultがnilになってしまうのはproduct_idの指定の仕方が間違えていたことによるものでした。
444
+
445
+ 上の方にも書いた通り、product_idは他のテーブルに値を探しにいって変数product_idに入れて、searchメソッドに渡しています。その時にフォームから直接受け取ったgroup_idは"1"となっていたのに対し、product_idは 1 となっていました。そのため、そのまま、product_idを検索条件に入れてしまうと、条件に引っかからず、検索結果が0となり、```ruby
446
+
447
+ [1] pry(#<Productions::SearchesController>)> product_id
448
+
449
+ => 1
450
+
451
+ [2] pry(#<Productions::SearchesController>)> params[:production][:group_id]
452
+
453
+ => "1"がnilになっているのでしたそこで下記のように、searchメソッドを書き直したところ、きちんと検索結果が、@production_resultに入ってくれました。
454
+
455
+
456
+
457
+ ```ruby
458
+
459
+ #production.rb
460
+
461
+ def self.search(date,line_id,product_id)
462
+
463
+ Production.where(date: date).
464
+
465
+ where(group_id: group_id).
466
+
467
+ where(product_id: "#{product_id}")
468
+
469
+ end
470
+
471
+ ```
472
+
473
+ ```ruby
474
+
475
+ [1] pry(#<Productions::SearchesController>)> product_id
476
+
477
+ => 1 #こっちはただの数字
478
+
479
+ [2] pry(#<Productions::SearchesController>)> params[:production][:group_id]
480
+
481
+ => "1" #こっちはダブルクォーテーションで囲まれている
482
+
483
+ ```
484
+
485
+
486
+
487
+ ただ、まだ検索結果のビューでは、@production_resulはnilclassになってしまいますね、、、。
488
+
489
+ うまく変数が渡せていないだけだと思うのですが、、、。

5

誤字の修正

2019/12/14 11:50

投稿

clora
clora

スコア72

test CHANGED
File without changes
test CHANGED
@@ -322,7 +322,7 @@
322
322
 
323
323
  Rails.application.routes.draw do
324
324
 
325
- root 'lines#index'
325
+ root 'groups#index'
326
326
 
327
327
  namespace :productions do
328
328
 

4

誤字の修正

2019/12/14 06:47

投稿

clora
clora

スコア72

test CHANGED
File without changes
test CHANGED
@@ -366,7 +366,7 @@
366
366
 
367
367
  product_id = Product.find_by(code: params[:production][:product_id]).id
368
368
 
369
- @production_result = Production.search(params[:production][:date], params[:production][:line_id], product_id)
369
+ @production_result = Production.search(params[:production][:date], params[:production][:group_id], product_id)
370
370
 
371
371
 
372
372
 
@@ -388,11 +388,11 @@
388
388
 
389
389
  def self.search(date,line_id,product_id)
390
390
 
391
- if date and line_id and product_id
391
+ if date and group_id and product_id
392
392
 
393
393
  Production.where
394
394
 
395
- ("(date = ?) AND (line_id = ?) AND (product_id = ?)", "%#{date}%", "%#{line_id}%", "%#{product_id}%")
395
+ ("(date = ?) AND (group_id = ?) AND (product_id = ?)", "%#{date}%", "%#{group_id}%", "%#{product_id}%")
396
396
 
397
397
  else
398
398
 

3

誤字の修正

2019/12/14 06:46

投稿

clora
clora

スコア72

test CHANGED
File without changes
test CHANGED
@@ -356,18 +356,6 @@
356
356
 
357
357
  ```
358
358
 
359
-
360
-
361
- resources :groups do
362
-
363
- resources :productions
364
-
365
- end
366
-
367
- end
368
-
369
-
370
-
371
359
  ```ruby
372
360
 
373
361
  #/controllers/productions/searches_controller.rb

2

コメントを受けて追記

2019/12/14 06:44

投稿

clora
clora

スコア72

test CHANGED
@@ -1 +1 @@
1
- [Ruby on Rails]複数条件でAND検索したい
1
+ [Ruby on Rails]複数条件でAND検索し、その結果をビューで表示させたい
test CHANGED
@@ -289,3 +289,159 @@
289
289
  rails 5.0.7
290
290
 
291
291
  ruby 2.5.1
292
+
293
+
294
+
295
+ ### 追記
296
+
297
+ コメントで指摘を受けた部分を変更しました。
298
+
299
+ それに加えて、resultアクションを追加して、@production_resultに検索結果を挿入し、resultアクションで検索結果を表示させるようにしました。
300
+
301
+ しかし、@production_resultがnilになってしまい検索結果が表示されません。
302
+
303
+ どうもsearchメソッドを使用すると、@production_resultがから([])となってしまいます。
304
+
305
+ これは、検索結果がないということなのでしょうか?配列の中の値がnilとして返ってくるのならわかるのですが、配列自体が空になってしまいます。まだ、searchメソッドの記述がおかしいのでしょうか。
306
+
307
+ 下記はsearchメソッドを使用した後、binding pryで確かめた結果です。
308
+
309
+ ```
310
+
311
+ [1] pry(Production)> @production_result
312
+
313
+ => nil
314
+
315
+ ```
316
+
317
+
318
+
319
+ ```ruby
320
+
321
+ #route.rb
322
+
323
+ Rails.application.routes.draw do
324
+
325
+ root 'lines#index'
326
+
327
+ namespace :productions do
328
+
329
+ resources :searches, only: :index do
330
+
331
+ collection do
332
+
333
+ get 'search'
334
+
335
+ end
336
+
337
+ collection do
338
+
339
+ get 'result'
340
+
341
+ end
342
+
343
+ end
344
+
345
+ end
346
+
347
+
348
+
349
+ resources :groups do
350
+
351
+ resources :productions
352
+
353
+ end
354
+
355
+ end
356
+
357
+ ```
358
+
359
+
360
+
361
+ resources :groups do
362
+
363
+ resources :productions
364
+
365
+ end
366
+
367
+ end
368
+
369
+
370
+
371
+ ```ruby
372
+
373
+ #/controllers/productions/searches_controller.rb
374
+
375
+ def search
376
+
377
+ @production_result = Production.new
378
+
379
+ product_id = Product.find_by(code: params[:production][:product_id]).id
380
+
381
+ @production_result = Production.search(params[:production][:date], params[:production][:line_id], product_id)
382
+
383
+
384
+
385
+ redirect_to result_productions_searches_path(@production_result)
386
+
387
+ end
388
+
389
+
390
+
391
+ def result
392
+
393
+
394
+
395
+ end
396
+
397
+ ```
398
+
399
+ ```ruby
400
+
401
+ def self.search(date,line_id,product_id)
402
+
403
+ if date and line_id and product_id
404
+
405
+ Production.where
406
+
407
+ ("(date = ?) AND (line_id = ?) AND (product_id = ?)", "%#{date}%", "%#{line_id}%", "%#{product_id}%")
408
+
409
+ else
410
+
411
+ redirect_to productions_searches_path
412
+
413
+ end
414
+
415
+ end
416
+
417
+ ```
418
+
419
+
420
+
421
+ ```html
422
+
423
+ -#views/productions/searches/result.html
424
+
425
+ 検索結果
426
+
427
+ <%= @production_result.date %>
428
+
429
+ <%= @production_result.group_id %>
430
+
431
+ <%= @production_result.product_id %>
432
+
433
+ ```
434
+
435
+
436
+
437
+ | id | date | group_id | product_id |
438
+
439
+ | ---- | ---- | ---- | ---- |
440
+
441
+ | 26 | 2019-12-13 00:00:00 | 1 | 1 |
442
+
443
+
444
+
445
+ ### 試したこと
446
+
447
+ binding pryをしてみたところ、seaechメソッドをすると@production_resultが[]となってしまいます。

1

誤字の修正

2019/12/14 06:42

投稿

clora
clora

スコア72

test CHANGED
File without changes
test CHANGED
@@ -182,7 +182,7 @@
182
182
 
183
183
  <%= f.number_field :products_id %> #productsのid(数字)を入力
184
184
 
185
- <%= f.select :group_id, [["8F-16", 1],["8F-9", 2]] %> #グループidの入力
185
+ <%= f.select :group_id, [["グループ1", 1],["グループ2", 2]] %> #グループidの入力
186
186
 
187
187
  <%= f.submit '検索', class: "btn" %>
188
188