Ransack搜索总是用";LIKE NULL";尽管接收到该值



我尝试使用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 %>

这是我的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)

我不明白它为什么使用";类似于null;。因此,它不会返回任何内容。

从Ransack文档中,_cont确实会执行LIKE查询

*_cont  Contains value  uses LIKE

因此,如果它是一个id,您可能希望使用相等谓词(_eq(,或者如果您正在针对多个id 进行搜索,则可能希望使用_in

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

最新更新