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! ++++ -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 15, 2012, at 4:34 PM, Dl33ter Yay wrote:> 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: @marques = Marque.order(:name) Walter> > If you need some of my code, juste ask what part you want. > > Thanks! > ++++ > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 : 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. Thanks for your answer. Bye! ++++ -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 16, 2012, at 7:44 AM, Dl33ter Yay wrote:> 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 : > 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> > Thanks for your answer. > Bye! > ++++ > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 [...]
Thank you again ;)
++++
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.