# Filtering and sorting

**URL:** <https://www.ruby-forum.com/t/filtering-and-sorting/143184>\
**Category:** Rails\
**Created:** [July 16, 2008, 2:43am UTC](https://www.ruby-forum.com/t/filtering-and-sorting/143184 "2008-07-16T02:43:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blakes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Blakes](https://www.ruby-forum.com/u/Blakes)\
**Post date:** [July 16, 2008, 2:43am UTC](https://www.ruby-forum.com/t/filtering-and-sorting/143184/1 "2008-07-16T02:43:21Z")

</div>

Hi all, I am still kind of new to the Ruby scene, and have run into a  
bit of a snag that I have been wrestling with all day. I have a Table  
that has a form that you use to filter information, and then renders  
the page again with the filtered information appearing in the table.  
Each column is able to be sorted. However, if I try to sort once a  
filter has been run, it defaults all the filter values to nil, and  
sorts the entire database of records.

this is in my Controller currently, and will work one or the other,  
but not both:  
if request.post?  
scope = NewRecord.scoped({})  
scope = scope.conditions “file\_bitrate = ?”,  
params[:file\_bitrate] unless params[:file\_bitrate] == ‘–Select  
Bitrate–’  
scope = scope.conditions “file\_quality = ?”,  
params[:file\_quality] unless params[:file\_quality] == ‘–Select  
Quality–’  
scope = scope.conditions “file\_samplerate = ?”,  
params[:file\_samplerate] unless params[:file\_samplerate] == ‘–Select  
Sample Rate–’  
scope = scope.conditions “file\_encoding = ?”,  
params[:file\_encoding] unless params[:file\_encoding] == ‘–Select  
Encoding–’  
scope = scope.conditions “file\_short = ?”, params[:file\_short]  
unless params[:file\_short] == ‘–Select Filename–’  
@new\_records = scope  
if params[:new\_codec] != ‘–Select Codec–’  
@new\_records.reject!{|rec| rec.pair.run.new\_codec !=  
params[:new\_codec]}  
end  
end

```
@new_records ||= NewRecord.find(:all)
 if params[:sort_key]
    @new_records = @new_records.sort_by {|record|

```

record.send(params[:sort\_key]) }  
if params[:sort\_reverse]  
@new\_records = @new\_records.reverse  
end  
end

If anyone has any tips on how to keep the current values of the  
filter, get the form to re-submit for a filter to trigger the filter,  
and then sort the filtered table, i would be really appreciative.  
Also, if anyone has a better way to do this, let me know, and I am  
open to change. I should note that I have no experience with js or  
AJAX.

Thanks so much.
