我想为:order
默认值:为可以覆盖的`:order`创建一个默认值?
class Comment < ActiveRecord::Base
default_scope :order => 'total_votes DESC'
我也让用户的评论进行排序:
posts_controller.rb:
@posts = current_user.subscribed_posts.paginate(:page => params[:page],
:per_page => 5,
:order => params[:order_by])
职位/ 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>
我按哪个链接并不重要,default_scope
没有被覆盖(无论如何,它保持为total_votes DESC
)。
这是默认行为吗?如果是这样,那么这种情况的正确解决方案是什么? (我想为:order
设置默认值,但只要点击show.html.erb
中的链接,就可以将其更改为:order_by
的值)。
你有没有经过这些: http://stackoverflow.com/questions/1834159/overriding-a-rails-default-scope http://stackoverflow.com/questions/1648971/rails-why-is-与独家范围的保护,任何好的实践上,如何使用的,它 – Nilesh 2012-03-12 11:42:44