<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>source.mihelac.org - Rails in practice - Group records by condition Comments</title>
  <id>tag:source.mihelac.org,2008:/2007/5/12/rails-in-practice-group-records-by-condition/comments</id>
  <generator version="0.7.3" uri="http://mephistoblog.com">Mephisto Noh-Varr</generator>
  <link href="http://source.mihelac.org/2007/5/12/rails-in-practice-group-records-by-condition/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/5/12/rails-in-practice-group-records-by-condition" rel="alternate" type="text/html"/>
  <updated>2008-02-01T22:56:52Z</updated>
  <entry xml:base="http://source.mihelac.org/">
    <author>
      <name>myname</name>
    </author>
    <id>tag:source.mihelac.org,2007-05-12:35:1263</id>
    <published>2008-02-01T22:56:52Z</published>
    <updated>2008-02-01T22:56:52Z</updated>
    <link href="http://source.mihelac.org/2007/5/12/rails-in-practice-group-records-by-condition" rel="alternate" type="text/html"/>
    <title>Comment on 'Rails in practice - Group records by condition' by myname</title>
<content type="html">&lt;p&gt;really nice one&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://source.mihelac.org/">
    <author>
      <name>Bojan Mihelac</name>
    </author>
    <id>tag:source.mihelac.org,2007-05-12:35:774</id>
    <published>2007-12-18T07:11:24Z</published>
    <updated>2007-12-18T07:11:24Z</updated>
    <link href="http://source.mihelac.org/2007/5/12/rails-in-practice-group-records-by-condition" rel="alternate" type="text/html"/>
    <title>Comment on 'Rails in practice - Group records by condition' by Bojan Mihelac</title>
<content type="html">&lt;p&gt;Bischov, what you have in block that you are sending to classify?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://source.mihelac.org/">
    <author>
      <name>astrid</name>
    </author>
    <id>tag:source.mihelac.org,2007-05-12:35:770</id>
    <published>2007-12-17T03:59:22Z</published>
    <updated>2007-12-17T03:59:22Z</updated>
    <link href="http://source.mihelac.org/2007/5/12/rails-in-practice-group-records-by-condition" rel="alternate" type="text/html"/>
    <title>Comment on 'Rails in practice - Group records by condition' by astrid</title>
<content type="html">&lt;p&gt;Good Day! To use credit cards or cash is the own choice of everyone. For me it is more convenient to purchase and travel with credit cards. I carry cash very seldom because today credit cards are available almost everywhere. I am going to Spain this vacation and yesterday applied for a new credit card at&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http:&quot; /&gt;discover card balance transfer check&lt;/a&gt;&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://source.mihelac.org/">
    <author>
      <name>Bischov</name>
    </author>
    <id>tag:source.mihelac.org,2007-05-12:35:759</id>
    <published>2007-11-28T11:59:52Z</published>
    <updated>2007-11-28T11:59:52Z</updated>
    <link href="http://source.mihelac.org/2007/5/12/rails-in-practice-group-records-by-condition" rel="alternate" type="text/html"/>
    <title>Comment on 'Rails in practice - Group records by condition' by Bischov</title>
<content type="html">&lt;p&gt;Hi,&lt;/p&gt;


	&lt;p&gt;thanks for this &#8211; it&#8217;s really needed a lot. But with the first method (.to_set.classify), I have a problem, it will only return the first group, instead of all the groups.&lt;/p&gt;


	&lt;p&gt;I am creating the sets on a varchar column like this:&lt;/p&gt;


	&lt;p&gt;week52
  title1
  title2
  title3
week51
  title1
  title2
week49
  title1
  title2
  title3&lt;/p&gt;


	&lt;p&gt;...and so on&#8230;with your example, only week52 is in the returned set.&lt;/p&gt;


	&lt;p&gt;when I use Brandon&#8217;s method, all groups are returned, but as you said it, unsorted, which is no good for me.&lt;/p&gt;


	&lt;p&gt;Please help!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://source.mihelac.org/">
    <author>
      <name>bmihelac</name>
    </author>
    <id>tag:source.mihelac.org,2007-05-12:35:168</id>
    <published>2007-07-15T08:01:57Z</published>
    <updated>2007-07-15T08:01:57Z</updated>
    <link href="http://source.mihelac.org/2007/5/12/rails-in-practice-group-records-by-condition" rel="alternate" type="text/html"/>
    <title>Comment on 'Rails in practice - Group records by condition' by bmihelac</title>
<content type="html">&lt;p&gt;Good point Brandon. &lt;ins&gt;group_by&lt;/ins&gt; method of Rails extension to &lt;ins&gt;Enumerable&lt;/ins&gt; can also be used (and is more readable too), but still it returns hash and not array. If you sort hash you would get nested array.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://source.mihelac.org/">
    <author>
      <name>Brandon Keepers</name>
    </author>
    <id>tag:source.mihelac.org,2007-05-12:35:167</id>
    <published>2007-07-14T19:43:40Z</published>
    <updated>2007-07-14T19:43:40Z</updated>
    <link href="http://source.mihelac.org/2007/5/12/rails-in-practice-group-records-by-condition" rel="alternate" type="text/html"/>
    <title>Comment on 'Rails in practice - Group records by condition' by Brandon Keepers</title>
<content type="html">&lt;p&gt;Enumberable also defines &lt;a href=&quot;http://api.rubyonrails.org/classes/Enumerable.html#M001098&quot;&gt;group_by&lt;/a&gt;, which does something similar, but uses an array of arrays which, unlike, hash, is ordered.&lt;/p&gt;


&lt;pre&gt;
@groups = NewsArticle.find(:all).group_by {|article| article.published_on.year}

&amp;lt;%@groups.each do |(year, articles)|%&amp;gt;

  &amp;lt;h1&amp;gt;&amp;lt;%=year%&amp;gt;&amp;lt;/h1&amp;gt;
  &amp;lt;%=articles.collect {|a| \&quot;&amp;lt;p&amp;gt;a.name&amp;lt;/p&amp;gt;\&quot;}.join%&amp;gt;
&amp;lt;%end%&amp;gt;
&lt;/pre&gt;</content>  </entry>
</feed>
