I'm facing an issue with Ransack search in my Rails application when attempting to filter results based on an enum field. Despite implementing what appears to be a correct setup, the search doesn't return the expected results when i select any transaction type. Here's a simplified version of my view and controller code related to the search functionality:
#Model code:
enum transaction_type: %i[LedgerBook StaffLedgerBook Transfer ExpenseEntry Order PurchaseSaleDetail Salary SalaryDetail Investment Expense]
#Views code:
<%= search_form_for @q, url: payments_path, class: "admin-controls" , method: "get" do |f| %>
<div class="card col-lg-12 shadow mb-3 p-3">
<div class="row mt-3">
<div class="col-lg-3 form-group float-left">
<%=f.select:account_id_in,options_from_collection_for_select(@accounts, "id" , ->(st){"#{st.title} \u{27F6}
#{st.bank_name}"},f.object.account_id_in), {:include_blank => "Select the Account Title"},{:multiple =>
true,:class=>"form-control chosen-select", :data => {:placeholder => "Select the Account Title"} } %>
</div>
<div class="col-lg-2 form-group float-left">
<%= f.search_field :id_eq, class: 'form-control input-sm ' , placeholder: "Enter Id" %>
</div>
<div class="col-lg-2 form-group float-left">
<%= f.select :transaction_type_eq, options_for_select(Payment.transaction_types.map { |key, value|
[key.humanize, value] }, @transaction_type),{:include_blank=> "Select Type"}, { class: "form-control
chosen-select", prompt: 'Select Type' }%>
</div>
<div class="ml-1">
<p>From:</p>
</div>
<div class="col-lg-2 form-group float-left">
<%= f.search_field :created_at_gteq,value: @start_date, class: 'form-control input-sm datepicker'
, 'datepicker'=> true,placeholder: "YYYY-MM-DD" %>
</div>
<div class="ml-1">
<p>To:</p>
</div>
<div class="col-lg-2 form-group float-left">
<%= f.search_field :created_at_lteq,value: @end_date, class: 'form-control input-sm datepicker' , 'datepicker'=>
true ,placeholder: "YYYY-MM-DD"%>
</div>
<div class="col-1 form-group float-left"> <button class="btn btn-primary" name="search_submit" type="submit"
value="Search" style="width:100px;"><i class="fa fa-search"></i></button> </div>
</div>
</div>
# Controller code :
if params[:q].present? @title = params[:q][:account_title_cont] @start_date = params[:q][:created_at_gteq] @end_date = params[:q][:created_at_lteq] @transaction_type = params[:q][:transaction_type_eq] params[:q][:created_at_lteq] = params[:q][:created_at_lteq].to_date.end_of_day if params[:q][:created_at_lteq].present? @q = Payment.where(max_credit: nil, max_debit: nil).ransack(params[:q]) else @q = Payment.where(created_at: @start_date.to_date.beginning_of_day..@end_date.to_date.end_of_day, max_credit: nil, max_debit: nil).ransack(params[:q])
end
In this code, I'm trying to filter payments based on an enum field transaction_type. However, the search results do not match the selected transaction type. I have confirmed that the @transaction_type variable is correctly assigned, and the Ransack parameter names seem to align with the attributes in my model. The options_for_select in the view also looks correct. Could someone please guide me on what might be causing this issue and how I can properly implement Ransack search with an enum field in Rails?
You can do something like this.