Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:24:31+00:00 2026-05-30T04:24:31+00:00

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

  • 0

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?

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.

But for the moment, Rails sort me like this (ID) : 1 – 2 – 3 // 3 – 2 – 1. That render this : VOLKSWAGEN – AUDI – BMW // BMW – AUDI / VOLKSWAGEN.

Like I said, I’m following this screencast :
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.

In my annonce_controller :

  helper_method :sort_column, :sort_direction
  def index
    @annonces = Annonce.order(sort_column + " " + sort_direction)
    @modeles = Modele.all
    @marques = Marque.order(sort_column + " " + sort_direction)
    @energies = Energy.all

  end
...
...
 private

  def sort_column
  Annonce.column_names.include?(params[:sort]) ? params[:sort] : 
"marque_id"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : 
"asc"
  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
  helper_method :sort_column, :sort_direction

  def index
    @marques = Marque.order(sort_column + " " + sort_direction)
  end
...
...
  private

  def sort_column
    Marque.column_names.include?(params[:sort]) ? params[:sort] : 
"marque"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : 
"asc"
  end
end

the annonce.rb model :

class Annonce < ActiveRecord::Base
  attr_accessible :marque_id, :modele_id, :price, :type, :energy_id, 
:power
  belongs_to :marque
  belongs_to :modele
  belongs_to :energy
end

the marque.rb model :

class Marque < ActiveRecord::Base
  attr_accessible :marque
  has_many :modeles
  has_many :annonces
end

The view : annonce : index.html.erb

<table>
  <tr>
  <th><%= sortable "marque_id" %></th>
    <th><%= sortable "modele_id" %></th>
    <th><%= sortable "price" %></th>
    <th><%= sortable "power" %></th>
    <th><%= link_to "Energy", :sort => "energy_id" %></th>


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

The view of marque look like this.

My tables looks like that :

Annonces:

    create_table :annonces do |t|
      t.integer :marque_id
      t.integer :modele_id
      t.string :price
      t.string :power
      t.string :energy

Marque:

    create_table :marques do |t|
      t.string :marque

      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 […]

Thanks!

++++

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:24:32+00:00Added an answer on May 30, 2026 at 4:24 am

    Thanks to Ryan, from RailsCasts who “point me into the right direction” 😉

    In my annonces_controller:

     def index
    @annonces = Annonce.joins(:marque, :modele, :energy).order(sort_column + "  " + sort_direction)
    @modeles = Modele.all
    @marques = Marque.all
    @energies = Energy.all
    
    end
    

    And I replaced this in the private section :

      private
    
      def sort_column
        params[:sort] || "marque" && "modele" && "energy"
      end
    

    Thanks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.