現在、ransackというgemを使って、検索機能を作成しています。
やりたいこと
サイトは、Q&Aサイトで、Questionに複数のAnswerがつき、そのanswerの数が多い順に検索結果を表示したいと考えています。
その中で、selectボックスを使い、以下のような表示をさせています。
現在のコード
ただし、以下のように設定していますが、うまく機能しません。どこが間違っているのかのご指摘と、修正点を教えていただけないでしょうか?
特に、述語(predicate) の使い方がわかりません。この場合どの述語を使えば良いのでしょうか?
特に、_gtの部分が適切かどうか、ご助言をお願いします。
他の資料を見ると、「関連先のモデル名(複数)_関連先のカラム名_述語」という書き方をしなければならないとあり、それに従っていますが、これでいいのかもわかりません。
question_controller.rb
def search # 検索オブジェクト @search = Question.ransack(params[:q]) # 検索結果 @result = @search.result(distinct: true).order(created_at: :desc) @answer_count = Question.joins(:answers).group("question_id").order('count(question_id) desc') end
_search_form.html.erb
<%= search_form_for(@search, url: search_questions_path, method: :get) do |f| %> <div class="field"> <div class="control"> <%= fa_icon "angle-right", class: "searchicon" %> <%= f.label :answers_question_id_in, "回答数", class: "question-search-category" %><br> <div class="select is-fullwidth"> <%= f.select :answers_question_id_gt, [['回答数', @answer_count]], include_blank: "全てを選択" %> </div> </div> </div> <% end %>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。