尽管收到值,Ransack 搜索始终使用 "LIKE NULL" 检查 SQL 语句

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

我尝试使用 https://www.botreetechnologies.com/blog/implementing-advanced-search-in-ruby-on-rails-with-ransack 实现搜索代码,但在我搜索时它没有 return 任何值。它传递了 cont 值,但 select 语句始终使用“LIKE NULL” 我想显示搜索到的记录 group_id 这是我控制器中的搜索:

      # 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

这是我的 index.html.erb 小组作业文件

<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 %>

这是当我 return 时我的 put 语句输出的内容:

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)

我不明白为什么它使用“Like null”。由于这个原因,它没有 returning 任何东西。

From Ransack documentation _cont 确实会执行 LIKE 查询

*_cont  Contains value  uses LIKE

因此,如果它是一个 ID,您可能想使用相等谓词 (_eq),或者如果您要搜索多个 ID,则可能要使用 _in

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