# sortable table by name instead of ID

**URL:** <https://discuss.rubyonrails.org/t/sortable-table-by-name-instead-of-id/62841>\
**Category:** rubyonrails-talk\
**Created:** [February 15, 2012, 9:34pm UTC](https://discuss.rubyonrails.org/t/sortable-table-by-name-instead-of-id/62841 "2012-02-15T21:34:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 15, 2012, 9:34pm UTC](https://discuss.rubyonrails.org/t/sortable-table-by-name-instead-of-id/62841/1 "2012-02-15T21:34:18Z")

</div>

Hi!

I'm a RoR beginner, and I have a problem with my "app".

I have a table called " annonces " and a table called " marques ". (It's like "post" and "category", for a blog app)

I'm following the screencast #228 of railscasts, but when I want to sort the "Marque" column, it sort by ID and not by name...

So the question is, how do I do a sortable table column, with multiple table?

If you need some of my code, juste ask what part you want.

Thanks!

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [February 15, 2012, 10:06pm UTC](https://discuss.rubyonrails.org/t/sortable-table-by-name-instead-of-id/62841/2 "2012-02-15T22:06:11Z")

</div>

> Hi!
> 
> I'm a RoR beginner, and I have a problem with my "app".
> 
> I have a table called " annonces " and a table called " marques ". (It's like "post" and "category", for a blog app)
> 
> I'm following the screencast #228 of railscasts, but when I want to sort the "Marque" column, it sort by ID and not by name...
> 
> So the question is, how do I do a sortable table column, with multiple table?

Could you clarify if you want this to be sortable in the sense of "drag to sort", in the sense of "click on a header to sort by that attribute", or simply that you want to get the records out in a particular order when you request them in your controller?

If you mean the last of these, then you could do something as simple as:

&nbsp;&nbsp;@marques = Marque.order(:name)

Walter

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 16, 2012, 12:44pm UTC](https://discuss.rubyonrails.org/t/sortable-table-by-name-instead-of-id/62841/3 "2012-02-16T12:44:52Z")

</div>

Hi!

It's more "click on a header to sort by that attribute".

For example, if you click on the name of the column (Marque) this should sort alphabetical ASC and DESC: AUDI - BMW - VOLKSWAGEN / Re-click : VOLKSWAGEN - BMW - AUDI.

Like I said in my 1st post, I'm following this screencast :

> **[\#228 Sortable Table Columns - RailsCasts](http://railscasts.com/episodes/228-sortable-table-columns)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

You can see on this page a thumbail of the result.

But in my case, it's with multiple table ( Marque, Modele, Energie...)

The screencast work perfectly when everything is in the same table.

But I want to do it with content from other table.

Again, if you need my code, juste ask what you want.

Thanks for your answer. Bye!

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [February 16, 2012, 4:43pm UTC](https://discuss.rubyonrails.org/t/sortable-table-by-name-instead-of-id/62841/4 "2012-02-16T16:43:24Z")

</div>

> Hi!
> 
> It's more "click on a header to sort by that attribute".
> 
> For example, if you click on the name of the column (Marque) this should sort alphabetical ASC and DESC: AUDI - BMW - VOLKSWAGEN / Re-click : VOLKSWAGEN - BMW - AUDI.
> 
> Like I said in my 1st post, I'm following this screencast : [#228 Sortable Table Columns - RailsCasts](http://railscasts.com/episodes/228-sortable-table-columns)
> 
> You can see on this page a thumbail of the result.
> 
> But in my case, it's with multiple table ( Marque, Modele, Energie...)
> 
> The screencast work perfectly when everything is in the same table.
> 
> But I want to do it with content from other table.
> 
> Again, if you need my code, juste ask what you want.

What does your controller look like, and what does your view look like where you build this table? If you're pulling from multiple models, you're doing that somewhere in the controller (I hope) and building an array for marques, models and energies. That's where the sorting needs to happen.

Walter

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 16, 2012, 6:48pm UTC](https://discuss.rubyonrails.org/t/sortable-table-by-name-instead-of-id/62841/5 "2012-02-16T18:48:52Z")

</div>

In my annonce\_controller :

&nbsp;&nbsp;helper\_method :sort\_column, :sort\_direction &nbsp;&nbsp;def index &nbsp;&nbsp;&nbsp;&nbsp;@annonces = Annonce.order(sort\_column + " " + sort\_direction) &nbsp;&nbsp;&nbsp;&nbsp;@modeles = Modele.all &nbsp;&nbsp;&nbsp;&nbsp;@marques = Marque.order(sort\_column + " " + sort\_direction) &nbsp;&nbsp;&nbsp;&nbsp;@energies = Energy.all

&nbsp;&nbsp;end ... ... private

&nbsp;&nbsp;def sort\_column &nbsp;&nbsp;Annonce.column\_names.include?(params[:sort]) ? params[:sort] : "marque\_id" &nbsp;&nbsp;end

&nbsp;&nbsp;def sort\_direction &nbsp;&nbsp;&nbsp;&nbsp;%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" &nbsp;&nbsp;end end

For the " def index " I tried : @annonces = Annonce.order(sort\_column + " " + sort\_direction) @marques = Marque.all

and

@annonces = Annonce.find(params[:id]) @marques = Marque.order(sort\_column + " " + sort\_direction)

In my marque\_controller :

class MarquesController \< ApplicationController &nbsp;&nbsp;helper\_method :sort\_column, :sort\_direction

&nbsp;&nbsp;def index &nbsp;&nbsp;&nbsp;&nbsp;@marques = Marque.order(sort\_column + " " + sort\_direction) &nbsp;&nbsp;end ... ... &nbsp;&nbsp;private

&nbsp;&nbsp;def sort\_column &nbsp;&nbsp;&nbsp;&nbsp;Marque.column\_names.include?(params[:sort]) ? params[:sort] : "marque" &nbsp;&nbsp;end

&nbsp;&nbsp;def sort\_direction &nbsp;&nbsp;&nbsp;&nbsp;%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" &nbsp;&nbsp;end end

the annonce.rb model : class Annonce \< ActiveRecord::Base &nbsp;&nbsp;attr\_accessible :marque\_id, :modele\_id, :price, :type, :energy\_id, :power &nbsp;&nbsp;belongs\_to :marque &nbsp;&nbsp;belongs\_to :modele &nbsp;&nbsp;belongs\_to :energy end the marque.rb model : class Marque \< ActiveRecord::Base &nbsp;&nbsp;attr\_accessible :marque &nbsp;&nbsp;has\_many :modeles &nbsp;&nbsp;has\_many :annonces end

The view : annonce : index.html.erb \<table\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;\<th\>\<%= sortable "marque\_id" %\>\</th\> &nbsp;&nbsp;&nbsp;&nbsp;\<th\>\<%= sortable "modele\_id" %\>\</th\> &nbsp;&nbsp;&nbsp;&nbsp;\<th\>\<%= sortable "price" %\>\</th\> &nbsp;&nbsp;&nbsp;&nbsp;\<th\>\<%= sortable "power" %\>\</th\> &nbsp;&nbsp;&nbsp;&nbsp;\<th\>\<%= link\_to "Energy", :sort =\> "energy\_id" %\>\</th\>

\<% for annonce in @annonces %\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= annonce.marque.marque %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= annonce.modele.try(:modele) %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td align="right"\>\<%= annonce.price %\> €\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td align="right"\>\<%= annonce.power %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= annonce.energy.try(:energy) %\>\</td\> &nbsp;&nbsp;\</tr\> \<% end %\> \</table\>

The view of marque look like this.

My tables looks like that :

Annonces: &nbsp;&nbsp;&nbsp;&nbsp;create\_table :annonces do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.integer :marque\_id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.integer :modele\_id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :price &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :power &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :energy

Marque: &nbsp;&nbsp;&nbsp;&nbsp;create\_table :marques do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :marque

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.timestamps

I hope this will help...

I'd create all this by doing : rails g scaffold marque marque:string Or : rails g scaffold annonce marque\_id:integer [...]

Thank you again 😉
