Everyone,
This may or may not be a repeat (my last message didn't reach the
group). Basically, Tom's solution solves the problem, but the same
solution is in a collection called Core Extensions for Ruby, which is
just awesome. The method that acts_as_toms (i crack myself up) is
'partition_by' which is an extension to Enumerable.
To install the extensions: `gem install -r extensions`
To use the extensions add this to your 'environment.rb' file: "require
'extensions/all'"
Imagine the following:
## CONTROLLER
updates = Update.find :all # has the attribute date
grouped_updates = updates.partition_by {|u| u.date} # this will group
updates by date
## VIEW
<% grouped_updates.each_pair do |date,updates| -%>
<h3><%= date.to_s %></h3>
<% updates.each do |update| -%>
<!-- your display code for updates -->
<% end -%>
<% end -%>
##
Use extensions, they kick ass!
-Jordan
On 11/22/2006, "Tom Werner" <[EMAIL PROTECTED]> wrote:
>Here's a little group method I just wrote on the Array class (and how to
>use it) that might serve your purpose well:
>
>class Array
> def group
> groups = {}
> self.each do |e|
> key = yield e
> groups[key] ||= []
> groups[key] << e
> end
> groups
> end
>end
>
>class Foo
> attr_accessor :year, :title
>
> def initialize(year, title)
> self.year = year
> self.title = title
> end
>end
>
>a = []
>a << Foo.new(2006, 'Alpha')
>a << Foo.new(2006, 'Beta')
>a << Foo.new(2007, 'Gamma')
>a << Foo.new(2008, 'Delta')
>
>ag = a.group { |e| e.year }
>
>ag.keys.sort.each do |year|
> puts year
> ag[year].each do |foo|
> puts ' ' + foo.title
> end
>end
>
>__END__
>
>2006
> Alpha
> Beta
>2007
> Gamma
>2008
> Delta
>_______________________________________________
>Sdruby mailing list
>[email protected]
>http://lists.sdruby.com/mailman/listinfo/sdruby
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby