DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

William has posted 8 posts at DZone. View Full User Profile

Rails Named_scope

10.19.2009
| 5763 views |
  • submit to reddit
        You got code like this

class User < ActiveRecord::Base
  named_scope :school_names, :conditions => {:requirement_type => Type::SCHOOL_NAMES}
end

you would like to group the named_scope by 'name', you should add this code

named_scope :group, lambda { |str| { :group => str } }

use like this

User.school_names.group("name")

    

Comments

William Notowidagdo replied on Wed, 2007/08/15 - 11:34pm

@mdeering I will give it a try. Thanks.

Snippets Manager replied on Sat, 2009/11/07 - 10:22pm

Opp jumped the gun... named_scope :group, lambda { |*args| { :group => args.first || "name asc" } }

Snippets Manager replied on Sat, 2009/11/07 - 10:22pm

Give it a default column and sort order. named_scope :group, lambda { |*args| { :group => str || "name asc" } }