0
votes

I am using Ransack to search a database with multiple fields. On the view side, I am pre-populating the default field values in my search form with the previous query, which is available in the view, from the controller as @q, using search_form_for @q.

Throughout the form, this is working successfully, but my field called deleted_eq always returns nil when I try to access it with f.object.deleted_eq to check the value. This is despite other field query values being returned properly in the same place using the same format, e.g. f.object.line_type_eq.

Is "deleted" a special field name in Ransack? All fields in my query are working as expected in the controller to return the correct results.

Changing the name of "deleted" would require a database migration and lots of code changes in the project, so I'd hope to check if it is a reserved name before I make all those changes for testing.

Edit for more info: Rails 5.2.1, Ransack 2.0.1

deleted_eq is a dropdown done with f.select with descriptive text option names that are mapped to 'true', 'false', and ''. So yes, ultimately I believe Ransack is handling it as a boolean.

<%= f.select :deleted_eq, options_for_select([['Active Records', 'false'],
['Deleted Records Only', 'true'], ['Active and Deleted Records', '']],
f.object.deleted_eq || 'false'), {}, { :class => 'form-control',
:onChange => "searchOnchange();" }  %>
1
Is deleted checkbox, a dropdown, etc. There is no reason specifically that deleted_eq would not have a value but ransack has specific ways of handling booleans, which I would assume is the case for deleted. See Here for more info - engineersmnky
I'll add this info in an edit also -- deleted_eq is a dropdown done with f.select with descriptive text option names that are mapped to 'true', 'false', and ''. So yes, ultimately I believe Ransack is handling it as a boolean. - David M
Please post the params from the log when the search is executed. Also the code for searchOnchange() would be beneficial - engineersmnky

1 Answers

0
votes

Figured this out.

It seems like deleted_eq can be nil if a blank value is supplied. I had the most luck adapting another solution I found online like so:

<%= f.select :deleted_eq, [['Active Records', 0], ['Deleted Records', 1]],
   { include_blank: 'All Records', selected: params[:q] ? params[:q][:deleted_eq] : 0 },
   {:class => 'form-control', :onChange => "searchOnchange();" } %>

It's a shame that the include_blank option ("All Records") always has to display as the first item in the dropdown, but since I'm able to choose what starts selected, and I can choose "Active Records", it's not the end of the world.