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

I modified Julien B.'s code in “How to paginate, sort, and search a
table using Ajax and Rails” (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!

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.

Kevin L. wrote:

I modified Julien B.'s code in “How to paginate, sort, and search a
table using Ajax and Rails” (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.

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” (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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs