SQL sort

in my list action, I setup the sort parameters as folows :

sort = case @params[‘sort’]
when “login” then “login DESC”
when “first_name” then “first_name DESC”
when “last_name” then “last_name DESC”
end

to be used in a ‘sort_link_helper’
def sort_link_helper(text, param)
key = param
key += “_reverse” if @params[:sort] == param
options = {
:url => {:action => ‘list’, :params => @params.merge({:sort =>
key, :page => nil})},
:update => ‘zList’,
:before => “Element.show(‘spinnerT’)”,
:success => “Element.hide(‘spinnerT’)”
}
html_options = {
:title => “Sort this column”,
:href => url_for(:action => ‘list’, :params =>
@params.merge({:sort => key, :page => nil}))
}
link_to_remote(text, options, html_options)
end

In the list view I display :
HEADERS
<td align=“center” <%= sort_td_class_helper “login” %>>
<%= sort_link_helper “Login”, “login” %>

<td align=“center” <%= sort_td_class_helper “role” %>>
<%= sort_link_helper “Role”, “role” %>

<td align=“center” <%= sort_td_class_helper “first_name” %>>
<%= sort_link_helper “First Name”, “first_name” %>

<td align=“center” <%= sort_td_class_helper “last_name” %>>
<%= sort_link_helper “Last Name”, “last_name” %>

<td align=“center” <%= sort_td_class_helper “trustee” %>>
<%= sort_link_helper “Trustee”, “trustee” %>

BODY

<%= h(user.login) %> <%= h(user.roles[0].name) %> #1st role is main role <%= h(user.first_name) %> <%= h(user.last_name) %>

user and roles are joined by a many-to-many relationship (roles_users
table)

Its running very well… but I would like to be able to sort on the role
column…
when “role” then “role DESC” doesn’t work obviously
I got a MYSQL error
Mysql::Error: Unknown column ‘role’ in ‘order clause’: SELECT * FROM
users roles ORDER BY role DESC LIMIT 0, 5

thanks for your lights…

kad

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