【问题标题】:Ransack search is always checking SQL statement with "LIKE NULL" despite receiving the value尽管接收到值,但 Ransack 搜索始终使用“LIKE NULL”检查 SQL 语句
【发布时间】:2021-05-23 21:27:19
【问题描述】:

我尝试使用https://www.botreetechnologies.com/blog/implementing-advanced-search-in-ruby-on-rails-with-ransack 实现搜索代码,但在搜索时它没有返回任何值。它传递了 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 %>

这是我的 puts 语句在我返回时输出的内容:

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”。由于这个原因,它没有返回任何东西。

【问题讨论】:

    标签: ruby-on-rails ruby rails-activerecord ransack


    【解决方案1】:

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

    *_cont  Contains value  uses LIKE
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多