<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.1.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to display a collection grouped by an attribute value in Rails</title>
	<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>
	<pubDate>Fri, 19 Mar 2010 05:34:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>

	<item>
		<title>By: Grant Hutchins</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-712</link>
		<author>Grant Hutchins</author>
		<pubDate>Sun, 19 Oct 2008 07:05:27 +0000</pubDate>
		<guid>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>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-713</link>
		<author>Frank</author>
		<pubDate>Sun, 19 Oct 2008 13:20:40 +0000</pubDate>
		<guid>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: Chess</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-714</link>
		<author>Chess</author>
		<pubDate>Mon, 20 Oct 2008 03:08:03 +0000</pubDate>
		<guid>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'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: Miquel Herrera</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-887</link>
		<author>Miquel Herrera</author>
		<pubDate>Mon, 28 Dec 2009 13:28:51 +0000</pubDate>
		<guid>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 =&#62; :quotes, :order =&#62; :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: Miquel Herrera</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comment-888</link>
		<author>Miquel Herrera</author>
		<pubDate>Mon, 28 Dec 2009 13:30:01 +0000</pubDate>
		<guid>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 <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>I hope you get the idea</p>
]]></content:encoded>
				</item>
</channel>
</rss>
