Named_scope in rails unique records?

♀尐吖头ヾ 提交于 2019-12-05 03:28:44

You probably want to explore the :group option for finders and named_scopes:

named_scope :unique_styles, :order => "title desc", :limit => 3, :group => "title"

For Rails 3 peeps you can do it daisy-chain style:

scope :unique_styles, order("title DESC")
                      .select("DISTINCT title")
                      .limit(3)

If really all you want is the titles, this oughta do it for MySQL. (I haven't looked into whether other engines support DISTINCT.)

named_scope :unique_styles, :select => 'DISTINCT title', :order => 'title desc', :limit => 3
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!