I wanted to create a default value for :order
:
class Comment < ActiveRecord::Base
default_scope :order => 'total_votes DESC'
I'm also letting users to sort the comments:
posts_controller.rb:
@posts = current_user.subscribed_posts.paginate(:page => params[:page],
:per_page => 5,
:order => params[:order_by])
posts/show.html.erb:
<span><%= link_to 'Date (DESC)', post_path(@post, :order_by => "created_at DESC") %></span>
<span><%= link_to 'Date (ASC)', post_path(@post, :order_by => "created_at ASC") %></span>
<span><%= link_to 'Votes', post_path(@post, :order_by => "total_votes DESC") %></span>
It doesn't matter which link I press, the default_scope
is not being overwritten (it stays as total_votes DESC
no matter what).
Is this the default behavior? If so, what's the right solution for this situation?
(I want to have a default value for :order
but be able to change it to the value of :order_by
once a link in show.html.erb
is clicked).