# How to sort columns from more than one table using Ajax?

**URL:** <https://www.ruby-forum.com/t/how-to-sort-columns-from-more-than-one-table-using-ajax/92415>\
**Category:** Rails\
**Created:** [April 19, 2007, 10:41am UTC](https://www.ruby-forum.com/t/how-to-sort-columns-from-more-than-one-table-using-ajax/92415 "2007-04-19T10:41:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [April 19, 2007, 10:41am UTC](https://www.ruby-forum.com/t/how-to-sort-columns-from-more-than-one-table-using-ajax/92415/1 "2007-04-19T10:41:27Z")

</div>

I modified Julien B.'s code in “How to paginate, sort, and search a  
table using Ajax and Rails” ([How to paginate, sort and search a table with Ajax and Rails · dev.nozav.org](http://dev.nozav.org/rails_ajax_table.html))  
a bit for my application. However, each row of record in my app  
consists of fields from 2 tables, events and locations. The list action  
is in my event\_controller so the sort works for the fields belonging to  
the events table but not those belonging to the locations table, which  
has a has\_many relationship with the events table. I tried a couple of  
things (as a newbie) to make the sorting works for all columns but to no  
avail. Is it possible to do this? Perhaps I need to sort the locations  
table in the location\_controller and then somehow combine the two?  
Please help!

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [April 19, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/how-to-sort-columns-from-more-than-one-table-using-ajax/92415/2 "2007-04-19T17:35:16Z")

</div>

some code would help. can you show e.g. the controller? generally i  
would say you have to include the table name in the property to sort  
by, e.g.

@events = Event.find(:all, :include =\> :location, :order =\>  
“location.name”)

so “location.name” (or “location.name\_reverse”) would be the param in  
the sort link to sort by location name.

i’m just guessing here what the problem might be, as i said show some  
code that might clear things up.

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [April 19, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/how-to-sort-columns-from-more-than-one-table-using-ajax/92415/3 "2007-04-19T18:36:33Z")

</div>

Kevin L. wrote:

> I modified Julien B.'s code in “How to paginate, sort, and search a  
> table using Ajax and Rails” ([How to paginate, sort and search a table with Ajax and Rails · dev.nozav.org](http://dev.nozav.org/rails_ajax_table.html))  
> a bit for my application. However, each row of record in my app  
> consists of fields from 2 tables, events and locations. The list action  
> is in my event\_controller so the sort works for the fields belonging to  
> the events table but not those belonging to the locations table, which  
> has a has\_many relationship with the events table. I tried a couple of  
> things (as a newbie) to make the sorting works for all columns but to no  
> avail. Is it possible to do this? Perhaps I need to sort the locations  
> table in the location\_controller and then somehow combine the two?  
> Please help!

You may add another parameter (e.g. table) in order to distinguish which  
table the user is updating like this:

def sort\_link\_helper(text, param, table, spinnerid)  
key = param  
key += " DESC" if params[:sort] == param

```
options = {
    :url => {:action => 'index', :params => params.merge({:sort => 

```

key, :table =\> table})},  
:update =\> table,  
:before =\> “Element.show(‘#{spinnerid}’)”,  
:success =\> “Element.hide(‘#{spinnerid}’)”  
}  
html\_options = {  
:title =\> “Sort by this field”,  
:href =\> url\_for(:action =\> ‘index’, :params =\>  
params.merge({:sort =\> key, :table =\> table}))  
}  
link\_to\_remote(text, options, html\_options)  
end

and use params[‘table’] in the controller for different pagination when  
request is xhr.

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [April 23, 2007, 7:28am UTC](https://www.ruby-forum.com/t/how-to-sort-columns-from-more-than-one-table-using-ajax/92415/4 "2007-04-23T07:28:56Z")

</div>

I modified sort\_link\_helper method as you suggested but isn’t clear on  
how to use params[‘table’] in the controller. I tried a lot of things  
(combinations) without success. As before, the sort on columns that  
belong to events table works but the sort on the column that belong to  
the locations table does not. Here is the relevant code:

# this is the Event model

# AND I think you can tell what I did wrong in the snippet of codes

below by

# looking at the model

class Event \< ActiveRecord::Base

belongs\_to :location  
acts\_as\_ferret :store\_class\_name =\> true, :additional\_fields =\>  
[:location\_city]

def location\_city  
return location.city  
end

end

# here is the snippet of my partial:

```
</tr>

```

 \<% @events.each do |e| %\> "\> \<% end %\> 

| \> \<%= sort\_link\_helper "City", "city", "locations" %\> |
| \<%= e.location\_city %\> |

# and here is the SNIPPET of my list action in the event\_controller.rb:

def list

```
sort = case @params['sort']

       when "location_city" then "location_city"
       when "location_city_reverse" then "location_city DESC"
 end

@events = Event.find(:all, :include => :location)

)

@events_pages, @events = paginate :events, :order => sort, 

```

:conditions =\> conditions, :per\_page =\> events\_per\_page

```
if request.xml_http_request?
   render :partial => "events_list", :layout => false, 

```

:additional\_fields =\> [:location\_city]  
end

I think my problem is I don’t know how to implement the  
:additional\_fields of the event model in the list action and the  
partial. Please help again!

Wai T. wrote:

> Kevin L. wrote:
> 
> > I modified Julien B.'s code in “How to paginate, sort, and search a  
> > table using Ajax and Rails” ([How to paginate, sort and search a table with Ajax and Rails · dev.nozav.org](http://dev.nozav.org/rails_ajax_table.html))  
> > a bit for my application. However, each row of record in my app  
> > consists of fields from 2 tables, events and locations. The list action  
> > is in my event\_controller so the sort works for the fields belonging to  
> > the events table but not those belonging to the locations table, which  
> > has a has\_many relationship with the events table. I tried a couple of  
> > things (as a newbie) to make the sorting works for all columns but to no  
> > avail. Is it possible to do this? Perhaps I need to sort the locations  
> > table in the location\_controller and then somehow combine the two?  
> > Please help!
> 
> You may add another parameter (e.g. table) in order to distinguish which  
> table the user is updating like this:
> 
> def sort\_link\_helper(text, param, table, spinnerid)  
> key = param  
> key += " DESC" if params[:sort] == param
> 
> ```
> options = {
> :url => {:action => 'index', :params => params.merge({:sort => 
> 
> ```
> 
> key, :table =\> table})},  
> :update =\> table,  
> :before =\> “Element.show(‘#{spinnerid}’)”,  
> :success =\> “Element.hide(‘#{spinnerid}’)”  
> }  
> html\_options = {  
> :title =\> “Sort by this field”,  
> :href =\> url\_for(:action =\> ‘index’, :params =\>  
> params.merge({:sort =\> key, :table =\> table}))  
> }  
> link\_to\_remote(text, options, html\_options)  
> end
> 
> and use params[‘table’] in the controller for different pagination when  
> request is xhr.
