#実現したいこと
①gem ransackを使い、練習日(:training_date)の検索をかけたい
②かつ、ソート機能で練習日の並べ替えを行いたい
#環境
・Ruby:2.6.5
・Rails:5.2.4.3
#試したこと
①gem ransackをインストール
②Git Hubのdocumentに倣い、下記のようにControllerを設定
Controller
1 def index 2 @records = current_user.records.includes(:practices).page(params[:page]).per(8) 3 @q = Record.ransack(params[:q]) 4 @search_records = @q.result(distinct: true) 5 end
③index画面にて、下記のように記述
slim
1h1 練習記録一覧 2.container 3 .form-group 4 = search_form_for @q do |f| 5 = f.date_field :training_date_cont 6 = f.submit "検索"
④画像の通りに出力され、検索ボタンを押すが、反応なし
⑤念のため、ログを確認すると、下記の通りとなっています。
おそらく、
・どのUserかを読み込み
・今ログインしているUserの中のrecordモデルから、recordテーブルを選択(?)
・順に、Record_idが、Xのpracticesモデルから、practicesテーブルを選択(?)して表示
と読んだのですが、そこから、どうすべきなのか考えが思い浮かばず、考え方や解決策をご教授頂ければ幸いです。
(なお、:contを、:eqに変更してもダメでした)
Started GET "/" for ::1 at 2020-06-17 17:18:33 +0900 Processing by RecordsController#index as HTML User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 3], ["LIMIT", 1]] ↳ /Users/user/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98 Rendering records/index.html.slim within layouts/application Rendered records/_flash_messages.html.slim (23.6ms) Record Load (0.5ms) SELECT "records".* FROM "records" WHERE "records"."user_id" = $1 LIMIT $2 OFFSET $3 [["user_id", "3"], ["LIMIT", 8], ["OFFSET", 0]] ↳ app/views/records/index.html.slim:27 Practice Load (1.2ms) SELECT "practices".* FROM "practices" WHERE "practices"."record_id" IN ($1, $2, $3, $4) [["record_id", 63], ["record_id", 64], ["record_id", 65], ["record_id", 68]] ↳ app/views/records/index.html.slim:27 (0.3ms) SELECT SUM("practices"."practice_time") FROM "practices" WHERE "practices"."record_id" = $1 [["record_id", 63]] ↳ app/views/records/index.html.slim:31 (0.3ms) SELECT SUM("practices"."practice_time") FROM "practices" WHERE "practices"."record_id" = $1 [["record_id", 64]] ↳ app/views/records/index.html.slim:31 (0.3ms) SELECT SUM("practices"."practice_time") FROM "practices" WHERE "practices"."record_id" = $1 [["record_id", 65]] ↳ app/views/records/index.html.slim:31 (0.2ms) SELECT SUM("practices"."practice_time") FROM "practices" WHERE "practices"."record_id" = $1 [["record_id", 68]] ↳ app/views/records/index.html.slim:31 Rendered records/index.html.slim within layouts/application (44.6ms) Rendered records/_header.html.slim (3.7ms) Completed 200 OK in 72ms (Views: 63.7ms | ActiveRecord: 3.5ms)
#コード(全部)
Controller
1class RecordsController < ApplicationController 2before_action :authenticate_user! 3 4 def index 5 @records = current_user.records.includes(:practices).page(params[:page]).per(8) 6 @q = Record.ransack(params[:q]) 7 @search_records = @q.result(distinct: true) 8 end 9 10 def show 11 @record = Record.find(params[:id]) 12 end 13 14 def new 15 @record = Record.new 16 output = @record.outputs.build 17 practice = @record.practices.build 18 task = @record.tasks.build 19 end 20 21 def create 22 @record = Record.new(record_params) 23 logger.info "###### #{@record.inspect}" 24 if @record.save 25 flash[:success] = "練習内容の登録が完了しました。" 26 redirect_to records_url 27 else 28 flash[:alert] = "登録に失敗しました。" 29 render :new 30 end 31 end 32 33 def edit 34 @record = Record.find_by(id: params[:id]) 35 end 36 37 def update 38 @record = Record.find_by(id: params[:id]) 39 if @record.update(record_params) 40 flash[:success] = "練習内容の更新が完了しました。" 41 redirect_to records_url 42 else 43 flash[:alert] = "更新に失敗しました。" 44 render :edit 45 end 46 end 47 48 def destroy 49 record = Record.find_by(id:params[:id]) 50 record.destroy 51 52 redirect_to root_path, notice: "練習記録を削除しました。" 53 end 54 55 def aggregate_result 56 @record = Record.find(params[:id]) 57 gon.data = Record.where(params[:practice_time]) 58 6.times do 59 # gon.data << rand(100.0) 60 end 61 end 62 63 private 64 65 def set_user 66 @user = current_user || User.new 67 end 68 69 def record_params 70 params.require(:record).permit(:record_id, :training_date, :learning_point, outputs_attributes:[:output_name, :id, :_destroy], practices_attributes:[:practice_item, :practice_time, :id, :_destroy], tasks_attributes:[:task_name, :id, :_destroy]).merge(user_id: current_user.id) 71 end 72end
index
1= render 'records/flash_messages' 2 3h1 練習記録一覧 4.container 5 .form-group 6 = search_form_for @q do |f| 7 = f.date_field :training_date_cont 8 = f.submit "検索" 9 10.d-flex 11 .col-4.text-align: center 12 = link_to '新規登録', new_record_path, class: 'btn btn-primary' 13 .col-4.text-align: center 14 = link_to 'リマインダー設定', new_record_path, class: 'btn btn-primary' 15 .col-4.text-align: center 16 = link_to 'レポートを見る', aggregate_result_record_path(current_user.id), class: 'btn btn-primary' 17 18table.table-hover.table-respnsive 19 thead 20 tr 21 th= sort_link(@q, :training_date, '練習日') 22 th= sort_link(@q, :created_at, '登録日時') 23 th 総練習時間 24 th 25 th 26 tbody 27 - @records.each do |record| 28 tr 29 td= link_to record.training_date, record_path(record) 30 td= record.created_at 31 td= record.practices.sum(:practice_time) 32 / td= total 33 td 34 = link_to '編集する', edit_record_path(record), class: 'btn btn-primary mr-3' 35 = link_to '削除する', record_path(record), method: :delete, data: { confirm: "練習記録を削除します。よろしいですか?" }, class: 'btn btn-danger' 36 37= paginate @records
#追記(DB設計)
親:Recordモデル
子:Practiceモデル
training_dateは、Recordの属性です。
ruby
1ActiveRecord::Schema.define(version: 2020_05_25_064157) do 2 3 create_table "records", force: :cascade do |t| 4 t.string "user_id" 5 t.text "learning_point" 6 t.date "training_date" 7 t.datetime "created_at", null: false 8 t.datetime "updated_at", null: false 9 end 10 11 create_table "practices", force: :cascade do |t| 12 t.string "practice_item" 13 t.integer "practice_time" 14 t.bigint "record_id" 15 t.datetime "created_at", null: false 16 t.datetime "updated_at", null: false 17 t.index ["record_id"], name: "index_practices_on_record_id" 18 end 19end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/17 13:39
2020/06/18 05:37
2020/06/18 07:48