user3754535 user3754535 - 13 days ago 9
Ruby Question

Rails - Ransack doesn't query

I have the folowing

Sale.rb

belongs_to :client
belongs_to :user

has_many :line_items, dependent: :destroy
has_many :products, through: :line_items
has_many :deposits

accepts_nested_attributes_for :line_items, reject_if: :all_blank, allow_destroy: true


sales_controller.rb

def index
@q = Sale.ransack(params[:q])
@sales = @q.result(distinct: true)
end


views/admin/sales/index.html.erb

<%= search_form_for @q, url: admin_sales_path, html: {class: "form-inline"} do |f| %>

<%= f.search_field :sku, placeholder: "SKU", class: "input perfil" %>
<%= f.submit 'search', class: "btn btn-primary" %>

<% end %>

<table class="table">
<thead>
<tr>
<th><%= sort_link @q, :sku, "SKU" %></th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @sales.each do |sale| %>

...
<% end %>
</tbody>
</table>


And finally this is the log from the terminal:

Started GET "/admin/sales?utf8=%E2%9C%93&q%5Bsku%5D=sample&commit=search" for ::1 at 2016-11-24 00:29:33 -0600
Processing by Admin::SalesController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"sku"=>"sample"}, "commit"=>"search"}
Sale Load (0.3ms) SELECT DISTINCT "sales".* FROM "sales"
Rendered admin/sales/index.html.erb within layouts/admin (7.5ms)
Completed 200 OK in 361ms (Views: 346.0ms | ActiveRecord: 1.6ms)


Everything seems to work fine, but nothing seems to be filtered.

As note I apply this same method for others models and they work fine.

Answer

Please use this

<%= f.search_field :sku_eq, placeholder: "SKU", class: "input perfil" %>

You have a little bit mistake