# Sorting columns in my view

**URL:** <https://www.ruby-forum.com/t/sorting-columns-in-my-view/136131>\
**Category:** Rails\
**Created:** [April 29, 2008, 9:10am UTC](https://www.ruby-forum.com/t/sorting-columns-in-my-view/136131 "2008-04-29T09:10:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtoly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@jtoly](https://www.ruby-forum.com/u/jtoly)\
**Post date:** [April 29, 2008, 9:10am UTC](https://www.ruby-forum.com/t/sorting-columns-in-my-view/136131/1 "2008-04-29T09:10:07Z")

</div>

What’s the current method for having column headings that when you click  
on them, sort your view according to that column heading? I used the  
sortable\_columns plug in in the past, but it seems a force-fit for Rails  
2.0 respond\_to … format.html

---

<div class="post-metadata">

**Author:** ![jtoly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@jtoly](https://www.ruby-forum.com/u/jtoly)\
**Post date:** [April 29, 2008, 9:41am UTC](https://www.ruby-forum.com/t/sorting-columns-in-my-view/136131/2 "2008-04-29T09:41:59Z")

</div>

Jim T. wrote:

> What’s the current method for having column headings that when you click  
> on them, sort your view according to that column heading? I used the  
> sortable\_columns plug in in the past, but it seems a force-fit for Rails  
> 2.0 respond\_to … format.html

I decided to use a helper method when I did this recently to account for  
columns which would be sorted on a related model’s value, rather than  
the id in the column. The helper I am using is:

def project\_sort\_header column\_name, display\_as = nil  
display\_as ||= column\_name.to\_s.humanize  
link\_to display\_as, project\_list\_path(:sort =\> column\_name)  
end

In my view, I have table headings defined thusly:

```
<th><%= project_sort_header 'groups.name', 'Group' %></th>
<th><%= project_sort_header :title %></th>
<th>Description</th>
<th><%= project_sort_header :priority %></th>
<th><%= project_sort_header 'users.username', 'Owner' %></th>

```

And in the controller:

order = params[:sort] || :priority  
@projects = Project.find ;all, :order =\> order, :include =\> …

For multiple models, the helpers can be DRYd by passing in the table  
type and using self.send and constructing the xxx\_list\_path method from  
the table type.

---

<div class="post-metadata">

**Author:** ![jtoly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@jtoly](https://www.ruby-forum.com/u/jtoly)\
**Post date:** [July 1, 2008, 11:32am UTC](https://www.ruby-forum.com/t/sorting-columns-in-my-view/136131/3 "2008-07-01T11:32:36Z")

</div>

i want to know more about sorting in ruby.i hav project and  
customer model.In my project listing i want to sort the table based  
on the column heading .so i want a method having column headings that  
when you click  
on them, sort view according to that column heading?.In my view i hav  
proj-id,customer name,location.start date etc…so many fields.  
I want to sort the entire tsble based on customer name.i want a detail  
explanation that how should my controller and view look like…how can  
i?can you help me.

---

<div class="post-metadata">

**Author:** ![jtoly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@jtoly](https://www.ruby-forum.com/u/jtoly)\
**Post date:** [July 1, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/sorting-columns-in-my-view/136131/4 "2008-07-01T20:09:04Z")

</div>

jokrish wrote:

> i want to know more about sorting in ruby.

Sorting is handled by your call to the model’s find method; so to make  
it user selectable, you use a conditions hash with the find method so  
you can insert the field name the user clicked on into the find  
conditions.

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

The explanation of Conditions is the second section on that page.

Check out Ryan B. Railcasts on the subject… here’s one that should  
give you some ideas:

> **[\#15 Fun with Find Conditions - RailsCasts](http://railscasts.com/episodes/15-fun-with-find-conditions)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
