[英]Rails, Ransack: How to search HABTM relationship for “all” matches instead of “any”
[英]Ransack showing all instead of filter
我試圖使用Ransack gem,但它返回所有12,000行而不是使用過濾器。
控制器:
def list @q = LmiHost.ransack(params[:q]) if !(params[:commit].nil?) then @computers = @q.result else @computers = LmiHost.where(:job_id => 121) end end
視圖:
<script type="text/javascript"> $(document).ready(function() { $("#computer_search_results").dataTable(); }); </script> <table id="computer_search_table"> <%= search_form_for @q do |f| %> <tr><td>Project:</td><td><%= f.select :job_id, @project_job_selector %></td></tr> <tr><td><%= f.submit 'Search' %></td></tr> <%end%> </table> <table id="computer_search_results"> <thead> <tr> <th>Lmi Name</th> <th>Service Tag</th> <th>Model</th> <th>Ship Date</th> <th>Warranty Date</th> <th>Project</th> <th>Invoice Name</th> </tr> </thead> <tbody> <% @computers.each do |computer| %> <tr> <td><%= computer.host_description %></td> <td><%= computer.host_service_tag %></td> <td><%= computer.host_model %></td> <td><%= computer.ship_date %></td> <td><%= computer.warranty_date %></td> <td><%= computer.job.name unless computer.job.nil? %></td> <td><%= computer.invoice_name %></td> </tr> <% end %> </tbody> </table>
在seraching后,URL看起來沒問題: URL
但它表現得像:
@computers = LmiHost.all
任何想法也為什么會發生這種情況?
你做錯了是你沒有指定一個合適的匹配器。 如果要選擇特定的作業ID,則應在表單中指明搜索匹配器。
例如,你說job_id
應該是job_id_eq
或者你想要它的匹配器
你應該在哪里獲得如下的Ransack搜索項目。
Ransack::Search<class: LmiHost, base: Grouping <conditions: [Condition <attributes: ["job_id"], predicate: eq, values: ["123"]>], combinator: and>>
在你的Ransack搜索中我沒有看到謂詞因此它返回所有的resutls。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.