I'm trying to add a paginator and a search. It works fine until I add a pg_search. pg_search goes to the right URL but does not filter the search result. For example if i type Vere Lee - http://localhost:3000/admin/dashboard?utf8=%E2%9C%93&search=Vera+Lee&commit= , it would stick to the same page without filtering. If I take away paginator, pg_search seems to work fine.
Is there a way to combine them both?
Controller
class AdminController < ApplicationController
before_action :require_login
def dashboard
@users = User.joins(:filled_documents).group("users.id").having("count(filled_documents.id) > ?",0).order("created_at ASC").paginate(page: params[:page], per_page: 5).perform_search(params[:search])
end
Views/dashboard
<header class="title">
<h1>Dashboard</h1>
</header>
<form>
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="medium-6 cell">
<label>Search Field
<%= form_tag(@users_path, method: "get") do %>
<%= text_field_tag :search, nil, placeholder: "Search either company, first or last name", class: "form-control" %>
<%= submit_tag "", style: "display: none;" %>
<% end %>
</label>
</div>
</div>
</div>
</form>
<%= will_paginate @users %>
<section class="wrapper">
<div class="span-10">
<% @users.each do |user| %>
<h4><a style="color: #000;" href='/admin/usershowinfo?id=<%= "#{user.id}" %>'><%= "#{user.first_name} #{user.last_name} - #{user.company_name}" %></a></h4>
<table class="table-minimal">
<thead>
<tr>
<th colspan=4>Name</th>
<th>State</th>
<th colspan=3>Actions</th>
</tr>
</thead>
<tbody>
<% user.filled_documents.each do |filled_document| %>
<tr>
<td colspan=4>
<%= "#{filled_document.name.empty? ? "Untitled" : filled_document.name}" %> <span class="badge-notice"><%= filled_document.comments.count %></span>
<br>
<i><%= filled_document.document.name %></i>
</td>
<td><%= filled_document.valid? ? "Complete" : "In Progress" %></td>
<td colspan=3>
<%= link_to 'View', document_filled_document_path(filled_document.document, filled_document), class: "btn" %>
<%= link_to 'Edit', edit_document_filled_document_path(filled_document.document, filled_document) , class: "btn"%>
<%= link_to 'Download', download_document_filled_document_path(filled_document.document, filled_document) , class: "btn" if filled_document.valid? %>
<%= link_to 'Delete', [filled_document.document, filled_document], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<hr>
<% end %>
</div>
</section>
<%= will_paginate @users %>
User model
class User < ActiveRecord::Base
include Clearance::User
has_many :filled_documents
include PgSearch
pg_search_scope :search,
against: [
:company_name,
:first_name,
:last_name
],
using: {
tsearch: {
prefix: true,
normalization: 2
}
}
belongs_to :organisation
validates_presence_of :first_name, :last_name, :company_name, :phone_number
def self.perform_search(keyword)
if keyword.present?
then User.search(keyword)
else User.all
end
end
def update_password newpass
puts "user.update_password called for #{first_name} #{last_name}"
self.password = newpass
save(validate: false)
end
def accept_new_tnc
return false if admin?
latest_signup_tnc = TermsAndCondition.signup.last
return true if self.accepted_tnc != latest_signup_tnc.pdf_hash_value
false
end
def update_accepted_tnc
latest_signup_tnc = TermsAndCondition.signup.last
self.accepted_tnc = latest_signup_tnc.pdf_hash_value
self.accepted_tnc_date = DateTime.now
self.save
end
def accept_legal_advice
latest_legal_advice_tnc = TermsAndCondition.legal_advice.last
self.accepted_legal_advice = latest_legal_advice_tnc.pdf_hash_value
self.accepted_legal_advice_date = DateTime.now
self.save
end
end
To get around joining and groupping you can use belongs_to :user, counter_cache:true
(also need a migration to add filled_documents_count
integer field to users table and initial values fill)
Also paginator is usually the last scope in chain:
@users = User.where("filled_documents_count>?", 0)
@users = @users.perform_search(params[:search]) if params[:search].present?
@users = @users.order("created_at ASC").paginate(page: params[:page], per_page: 5)
But note that counter_cache
does not perform well when there can be multiple concurrent writes to same users's documents.