Home>
I want to achieve

① Use gem ransack to search for practice days (:training_date)
② And I want to sort the practice days with the sort function

environment

・Ruby: 2.6.5
・Rails:5.2.4.3

What I tried

① Install gem ransack
② Following the Git Hub document, set the Controller as follows

def index
    @records = current_user.records.includes(:practices).page(params[:page]).per(8)
    @q = Record.ransack(params[:q])
    @search_records = @q.result(distinct: true)
  end

③ On the index screen, describe as follows

h1 practice record list
.container
  .form-group
    = search_form_for @q do |f|
      = f.date_field :training_date_cont
      = f.submit "Search"

④ Output as shown in the image, press the search button, but no response

⑤ As a precaution, when you check the log, it is as follows.

Probably
・Read which User
・Select the record table from the record model in the currently logged in User (?)
-In order, Record_id is displayed by selecting (?) the practices table from the practices model of X.

I would like to thank you for coming up with an idea of ​​what should be done, and for teaching us your ideas and solutions.
(Note that changing :cont to :eq did not work)

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)
Code (all)
class RecordsController</pre>
<pre><code data-language="index">= render'records/flash_messages'
h1 practice record list
.container
  .form-group
    = search_form_for @q do |f|
      = f.date_field :training_date_cont
      = f.submit "Search"
.d-flex
  .col-4.text-align: center
    = link_to'New registration', new_record_path, class:'btn btn-primary'
  .col-4.text-align: center
    = link_to'Reminder settings', new_record_path, class:'btn btn-primary'
  .col-4.text-align: center
    = link_to'view report', aggregate_result_record_path(current_user.id), class:'btn btn-primary'
table.table-hover.table-respnsive
  thead
    tr
      th= sort_link(@q, :training_date,'practice date')
      th= sort_link(@q, :created_at,'registration date'))
      th total practice time
      th
      th
  tbody
    [email protected] do |record|
        tr
          td= link_to record.training_date, record_path(record)
          td= record.created_at
          td= record.practices.sum(:practice_time)
         /td= total
          td
            = link_to'Edit', edit_record_path(record), class:'btn btn-primary mr-3'
            = link_to'Delete', record_path(record), method: :delete, data: {confirm: "Delete the practice record. Are you sure?" },

 class:'btn btn-danger'
= paginate @records
Addition (DB design)

Parent: Record model
Child: Practice model
training_date is an attribute of Record.

ActiveRecord::Schema.define(version: 2020_05_25_064157) do
  create_table "records", force: :cascade do |t|
    t.string "user_id"
    t.text "learning_point"
    t.date "training_date"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
  create_table "practices", force: :cascade do |t|
    t.string "practice_item"
    t.integer "practice_time"
    t.bigint "record_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["record_id"],

 name: "index_practices_on_record_id"
  end
end
  • Answer # 1

    Is training_date not for User but for Practice?

    Then
    practices_training_date_cont.

    Addendum
    Not @records, @search_records,[email protected] do |record|To use
    @search_records = @q.result(distinct:true).includes(:practices).page(params[:page]).per(8)

Related articles