<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to display a collection grouped by an attribute value in Rails</title>
	<atom:link href="http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/</link>
	<description>Because programming should be fun</description>
	<lastBuildDate>Thu, 15 Jul 2010 19:48:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Miquel Herrera</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/comment-page-1/#comment-888</link>
		<dc:creator>Miquel Herrera</dc:creator>
		<pubDate>Mon, 28 Dec 2009 13:30:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-888</guid>
		<description>The form eaten my html example :(

I hope you get the idea</description>
		<content:encoded><![CDATA[<p>The form eaten my html example :(</p>
<p>I hope you get the idea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Miquel Herrera</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/comment-page-1/#comment-887</link>
		<dc:creator>Miquel Herrera</dc:creator>
		<pubDate>Mon, 28 Dec 2009 13:28:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-887</guid>
		<description>It is always interesting to learn new tricks but in this case I would consider more efficient to get the data ready to use straight from the DB rather than getting the same data and reorganize in memory. I would do something like this:

@authors = Author.find :all, :include =&gt; :quotes, :order =&gt; :name

this returns an array of authors and pre-load their quotes using two sql instances such as:

SELECT * FROM `authors` ORDER BY name;
SELECT `quotes`.* FROM `quotes` WHERE (`quotes`.author_id IN (1,2,3,...));

in the view you just loop all authors and their quotes:


    
	
	    
  	
  </description>
		<content:encoded><![CDATA[<p>It is always interesting to learn new tricks but in this case I would consider more efficient to get the data ready to use straight from the DB rather than getting the same data and reorganize in memory. I would do something like this:</p>
<p>@authors = Author.find :all, :include =&gt; :quotes, :order =&gt; :name</p>
<p>this returns an array of authors and pre-load their quotes using two sql instances such as:</p>
<p>SELECT * FROM `authors` ORDER BY name;<br />
SELECT `quotes`.* FROM `quotes` WHERE (`quotes`.author_id IN (1,2,3,&#8230;));</p>
<p>in the view you just loop all authors and their quotes:</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chess</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/comment-page-1/#comment-714</link>
		<dc:creator>Chess</dc:creator>
		<pubDate>Mon, 20 Oct 2008 03:08:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-714</guid>
		<description>Thanks for this, very helpful for us who don&#039;t yet know all the nooks and crannys in rails and ruby!</description>
		<content:encoded><![CDATA[<p>Thanks for this, very helpful for us who don&#8217;t yet know all the nooks and crannys in rails and ruby!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/comment-page-1/#comment-713</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Sun, 19 Oct 2008 13:20:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-713</guid>
		<description>Grant, this is even better than @authors.keys.sort.each, thanks for sharing.</description>
		<content:encoded><![CDATA[<p>Grant, this is even better than @authors.keys.sort.each, thanks for sharing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grant Hutchins</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/comment-page-1/#comment-712</link>
		<dc:creator>Grant Hutchins</dc:creator>
		<pubDate>Sun, 19 Oct 2008 07:05:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-712</guid>
		<description>You can also do:

@authors.sort.each do &#124;author_name, quote&#124;

because Hash#sort returns an array of 2-value arrays of the form [key, value]</description>
		<content:encoded><![CDATA[<p>You can also do:</p>
<p>@authors.sort.each do |author_name, quote|</p>
<p>because Hash#sort returns an array of 2-value arrays of the form [key, value]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
