开发者

rails3 unscoped within the model gets overriden by default_scope

I have this model

开发者_StackOverflow社区

User.rb

default_scope :order => 'users.created_at DESC'

and

scope :ranking, lambda { unscoped { order('users.ranking DESC') }}

and still I get a to_sql that includes ORDER BY users.created_at DESC, users.ranking DESC...

can someone explain why?

I really don't want to have to call unscoped from every controller i'll be using this model in.

Thanks!


As you're discovering, default_scope is often more trouble than it's worth. If you're wanting to stick with it, you could use reorder to ignore the previous order:

scope :ranking, reorder("ranking DESC")


Not sure why @TimPost deleted my answer but I'm using rails 3.0.5 and ruby 1.9.2 for a project and when I used reorder(which works btw) it says this in the log

DEPRECATION WARNING: reorder is deprecated. Please use except(:order).order(...) instead. (called from <class:Item>

So I don't think it is fair my answer was deleted and I got dinged for a crappy response

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜