Hi there,
I’m trying to grab a random row out of a database and I keep getting the
error:
Couldn’t find Reason with ID=random
Here’s my controller:
@reason = Reason.find(:first, :order => [“RAND()”])
I think that’s right, right? I know that RAND() is an expensive
operation, but it’s a small table so I’m not too worried.
Thanks!
Reason.find(Reason.find(:all, :select => :id).map(&:id).rand)
Enjoy.
On Wed, Mar 12, 2008 at 12:20 PM, Dave A. <
[email protected]> wrote:
@reason = Reason.find(:first, :order => [“RAND()”])
I think that’s right, right? I know that RAND() is an expensive
operation, but it’s a small table so I’m not too worried.
Thanks!
Posted via http://www.ruby-forum.com/.
–
Ryan B.
Feel free to add me to MSN and/or GTalk as this email.
On 11-Mar-08, at 9:54 PM, Ryan B. (Radar) wrote:
the
error:
Couldn’t find Reason with ID=random
Here’s my controller:
@reason = Reason.find(:first, :order => [“RAND()”])
I think that’s right, right? I know that RAND() is an expensive
operation, but it’s a small table so I’m not too worried.
or…
@reason = Reason.find(:all, :order => “RAND()”, :limit => 1)
(you might need to do a .first on the result set)
Jodi
Hmm… I tried what you have to a T (including the .first) and no luck.
Weird! I’ll keep trying stuff.
Jodi S. wrote:
On 11-Mar-08, at 9:54 PM, Ryan B. (Radar) wrote:
the
error:
Couldn’t find Reason with ID=random
Here’s my controller:
@reason = Reason.find(:first, :order => [“RAND()”])
I think that’s right, right? I know that RAND() is an expensive
operation, but it’s a small table so I’m not too worried.
or…
@reason = Reason.find(:all, :order => “RAND()”, :limit => 1)
(you might need to do a .first on the result set)
Jodi
Thanks for the patience. I figured it out, and it was an unrelated
routing error. Grr. 
Jodi S. wrote:
On 11-Mar-08, at 10:08 PM, Dave A. wrote:
Hey Dave -
error:
or…
@reason = Reason.find(:all, :order => “RAND()”, :limit => 1)
(you might need to do a .first on the result set)
Jodi
I’m doing that with rails 1.2.3 and mysql 5 (with a few :conditions
and :includes thrown in as well).
Jodi
On 11-Mar-08, at 10:08 PM, Dave A. wrote:
Hey Dave -
error:
or…
@reason = Reason.find(:all, :order => “RAND()”, :limit => 1)
(you might need to do a .first on the result set)
Jodi
I’m doing that with rails 1.2.3 and mysql 5 (with a few :conditions
and :includes thrown in as well).
Jodi
You could also try using the find_random plugin:
http://www.railslodge.com/plugins/964-find-random
I have not tried using it yet, but it looks well-executed. Has anyone
tried using the find_random plugin with has_finder?
Jason Arora
On Mar 11, 8:33 pm, Dave A. [email protected]