<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Random Records in Rails</title>
	<atom:link href="http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/</link>
	<description>A chess-playing machine of the late 18th century, promoted as an automaton but later proved a hoax.</description>
	<lastBuildDate>Mon, 30 Jan 2012 07:56:56 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: madmaurice</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-609</link>
		<dc:creator><![CDATA[madmaurice]]></dc:creator>
		<pubDate>Mon, 12 Dec 2011 01:22:48 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-609</guid>
		<description><![CDATA[what about &lt;Model&gt;.all.order(&quot;RAND()&quot;).first for MySQL based databases :) ]]></description>
		<content:encoded><![CDATA[<p>what about &lt;Model&gt;.all.order(&quot;RAND()&quot;).first for MySQL based databases <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: britt</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-608</link>
		<dc:creator><![CDATA[britt]]></dc:creator>
		<pubDate>Wed, 30 Mar 2011 19:18:01 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-608</guid>
		<description><![CDATA[I like Gordy&#039;s approach best!  ]]></description>
		<content:encoded><![CDATA[<p>I like Gordy&#039;s approach best!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-607</link>
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Fri, 26 Feb 2010 18:17:17 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-607</guid>
		<description><![CDATA[The bottom of my message looks foobar-ed.
 
1) RANDOM() for postges.  I prefer postges
 
2) conditions created_at &lt; Time.now SHOULD make sure DB caching will not mess with you, (the SQL will be different on every call)
 
3) *args used so that the value it optional (for people not using Ruby 1.9 yet.) ]]></description>
		<content:encoded><![CDATA[<p>The bottom of my message looks foobar-ed.</p>
<p>1) RANDOM() for postges.  I prefer postges</p>
<p>2) conditions created_at &lt; Time.now SHOULD make sure DB caching will not mess with you, (the SQL will be different on every call)</p>
<p>3) *args used so that the value it optional (for people not using Ruby 1.9 yet.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-606</link>
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Fri, 26 Feb 2010 18:11:29 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-606</guid>
		<description><![CDATA[If you are wooried about caching couldn&#039;t you add a simple date into the lookup?
 

 
named_scope :random, lambda { &#124;*args&#124; {:order =&gt; &quot;RANDOM()&quot;, :limit =&gt; (args.first &#124;&#124; 1), :conditions =&gt; [&quot;created_at &lt; ?&quot;, Time.now.utc] }}
 

 
changes:
 
1) (postgres uses random() not rand(), so I changed it to random())
 
2) conditions created_at  RANDOM().  that really helped me finish this in my project. :) ]]></description>
		<content:encoded><![CDATA[<p>If you are wooried about caching couldn&#039;t you add a simple date into the lookup?</p>
<p>named_scope :random, lambda { |*args| {:order =&gt; &quot;RANDOM()&quot;, :limit =&gt; (args.first || 1), :conditions =&gt; [&quot;created_at &lt; ?&quot;, Time.now.utc] }}</p>
<p>changes:</p>
<p>1) (postgres uses random() not rand(), so I changed it to random())</p>
<p>2) conditions created_at  RANDOM().  that really helped me finish this in my project. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damir</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-605</link>
		<dc:creator><![CDATA[Damir]]></dc:creator>
		<pubDate>Fri, 04 Dec 2009 11:47:47 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-605</guid>
		<description><![CDATA[How about this:
 

 
@posts = Post.find(:all).sort_by{rand} ]]></description>
		<content:encoded><![CDATA[<p>How about this:</p>
<p>@posts = Post.find(:all).sort_by{rand}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ruby on Rails: Get some random records &#124; David Lowry</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-604</link>
		<dc:creator><![CDATA[Ruby on Rails: Get some random records &#124; David Lowry]]></dc:creator>
		<pubDate>Mon, 30 Nov 2009 13:04:52 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-604</guid>
		<description><![CDATA[[...] from a baseline of the code found here at almosteffortless.com I&#8217;ve extended a &#8216;random record grabber&#8217; to get a specific number of unique [...] ]]></description>
		<content:encoded><![CDATA[<p>[...] from a baseline of the code found here at almosteffortless.com I&#8217;ve extended a &#8216;random record grabber&#8217; to get a specific number of unique [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Glenn</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-603</link>
		<dc:creator><![CDATA[Glenn]]></dc:creator>
		<pubDate>Fri, 13 Nov 2009 15:58:01 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-603</guid>
		<description><![CDATA[Dan has a cool use of named_scopes, but unfortunately I think the caching system might come up to bite you.  If you ask for Article.random(1) twice for instance, I believe it returns the same record. ]]></description>
		<content:encoded><![CDATA[<p>Dan has a cool use of named_scopes, but unfortunately I think the caching system might come up to bite you.  If you ask for Article.random(1) twice for instance, I believe it returns the same record.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-602</link>
		<dc:creator><![CDATA[Dan]]></dc:creator>
		<pubDate>Wed, 07 Oct 2009 15:10:25 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-602</guid>
		<description><![CDATA[I use a named_scope:
 

 
  named_scope :random, lambda { &#124;random&#124; {:order =&gt; &quot;RAND()&quot;, :limit =&gt; random }}
 

 
Article.random(4) would return 4 random objects ]]></description>
		<content:encoded><![CDATA[<p>I use a named_scope:</p>
<p>  named_scope :random, lambda { |random| {:order =&gt; &quot;RAND()&quot;, :limit =&gt; random }}</p>
<p>Article.random(4) would return 4 random objects</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: itchy</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-601</link>
		<dc:creator><![CDATA[itchy]]></dc:creator>
		<pubDate>Fri, 17 Jul 2009 13:44:08 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-601</guid>
		<description><![CDATA[To add to Benson&#039;s short solution, I needed to scope by &quot;active = 1&quot;, so:
 

 
  named_scope :active, :conditions =&gt; {:active =&gt; true}
 
    
 
  def self.random
 
    self.active.find(:first, :order =&gt; &#039;RAND()&#039;)
 
  end ]]></description>
		<content:encoded><![CDATA[<p>To add to Benson&#039;s short solution, I needed to scope by &quot;active = 1&quot;, so:</p>
<p>  named_scope :active, :conditions =&gt; {:active =&gt; true}</p>
<p>  def self.random</p>
<p>    self.active.find(:first, <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder =&gt; &#039;RAND()&#039;)</p>
<p>  end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benson</title>
		<link>http://trevorturk.wordpress.com/2007/12/04/random-records-in-rails/#comment-600</link>
		<dc:creator><![CDATA[Benson]]></dc:creator>
		<pubDate>Thu, 16 Jul 2009 02:01:15 +0000</pubDate>
		<guid isPermaLink="false">http://almosteffortless.com/2007/12/04/random-records-in-rails/#comment-600</guid>
		<description><![CDATA[Found this in another forum:
 

 
def self.random
 
  find(:first, :order =&gt; &quot;RAND()&quot;)
 
end ]]></description>
		<content:encoded><![CDATA[<p>Found this in another forum:</p>
<p>def self.random</p>
<p>  find(:first, <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder =&gt; &quot;RAND()&quot;)</p>
<p>end</p>
]]></content:encoded>
	</item>
</channel>
</rss>

