Search code examples
ruby-on-railsrubyrails-activerecordransack

Ransack search is always checking SQL statement with "LIKE NULL" despite receiving the value


I have tried to implement the search code using https://www.botreetechnologies.com/blog/implementing-advanced-search-in-ruby-on-rails-with-ransack but it does not return any values when I search. It passes the cont value but the select statement always uses "LIKE NULL" I want to display records with the searched group_id This is the search in my controller:

      # GET /group_assignments or /group_assignments.json
  def index
   # @q = GroupAssignment.ransack(params[:q])
    #@groups = @q.result
    puts params[:q]
    @q = GroupAssignment.ransack(params[:q])
    @group_assignments = @q.result
    puts @group_assignments
    #@group_assignments = GroupAssignment.all
  end

This is my index.html.erb file for the group assignments

<p id="notice"><%= notice %></p>

<h1>Group Assignments</h1>

<%= search_form_for @q do |f| %>
 <%= f.label :group_id_cont, 'Group ID' %>
 <%= f.search_field :group_id_cont %>
 <%= f.submit %>
<% end %>


<table>
  <thead>
    <tr>
      <th>Group ID</th>
      <th>Student ID</th>
      <th>Group Year</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @group_assignments.each do |group_assignment| %>
      <tr>
        <td><%= group_assignment.group_id %></td>
        <td><%= group_assignment.StudentID %></td>
        <td><%= group_assignment.GroupYear %></td>
        <td><%= link_to 'Show', group_assignment %></td>
        <td><%= link_to 'Edit', edit_group_assignment_path(group_assignment) %></td>
        <td><%= link_to 'Delete', group_assignment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Group Assignment', new_group_assignment_path %>
<%= link_to 'Back', groups_path %>

This is what my puts statement outputs when I return it:

Started GET "/group_assignments?q%5Bgroup_id_cont%5D=2&commit=Search" for ::1 at 2021-05-23 23:14:05 +0200
Processing by GroupAssignmentsController#index as HTML
  Parameters: {"q"=>{"group_id_cont"=>"2"}, "commit"=>"Search"}
  GroupAssignment Load (2.1ms)  SELECT `group_assignments`.* FROM `group_assignments` WHERE `group_assignments`.`group_id` LIKE NULL
  ↳ app/controllers/group_assignments_controller.rb:12:in `puts'
  Rendering layout layouts/application.html.erb
  Rendering group_assignments/index.html.erb within layouts/application
  Rendered group_assignments/index.html.erb within layouts/application (Duration: 1.7ms | Allocations: 376)
[Webpacker] Everything's up-to-date. Nothing to do
  Admin Load (2.0ms)  SELECT `admins`.* FROM `admins` WHERE `admins`.`id` = 1 ORDER BY `admins`.`id` ASC LIMIT 1
  ↳ app/views/layouts/application.html.erb:38
  Rendered home/_header.html.erb (Duration: 0.4ms | Allocations: 292)
  Rendered home/_footer.html.erb (Duration: 0.1ms | Allocations: 39)
  Rendered layout layouts/application.html.erb (Duration: 79.9ms | Allocations: 10808)
Completed 200 OK in 91ms (Views: 81.3ms | ActiveRecord: 4.2ms | Allocations: 12210)

I don't understand why it is using "Like null". It is not returning anything due to this.


Solution

  • From Ransack documentation _cont will indeed do a LIKE query

    *_cont  Contains value  uses LIKE
    

    So you probably want to use equality predicate (_eq) if it's one id, or perhaps _in if you you are searching against several ids

     <%= f.label :group_id_eq, 'Group ID' %>