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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reason.find(Reason.find(:all, :select => :id).map(&:id).rand) Enjoy. On Wed, Mar 12, 2008 at 12:20 PM, Dave Amos < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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! > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11-Mar-08, at 9:54 PM, Ryan Bigg (Radar) wrote:> Reason.find(Reason.find(:all, :select => :id).map(&:id).rand) > > Enjoy. > > On Wed, Mar 12, 2008 at 12:20 PM, Dave Amos <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > wrote: > > 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.or.. @reason = Reason.find(:all, :order => "RAND()", :limit => 1) (you might need to do a .first on the result set) Jodi --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hmm... I tried what you have to a T (including the .first) and no luck. Weird! I''ll keep trying stuff. Jodi Showers wrote:> On 11-Mar-08, at 9:54 PM, Ryan Bigg (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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11-Mar-08, at 10:08 PM, Dave Amos wrote: Hey Dave -> > Hmm... I tried what you have to a T (including the .first) and no > luck. > Weird! I''ll keep trying stuff. > > > Jodi Showers wrote: >> On 11-Mar-08, at 9:54 PM, Ryan Bigg (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) >> JodiI''m doing that with rails 1.2.3 and mysql 5 (with a few :conditions and :includes thrown in as well). Jodi --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the patience. I figured it out, and it was an unrelated routing error. Grr. :) Jodi Showers wrote:> On 11-Mar-08, at 10:08 PM, Dave Amos 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Amos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks for the patience. I figured it out, and it was an unrelated > routing error. Grr. :) > > > > Jodi Showers wrote: > > On 11-Mar-08, at 10:08 PM, Dave Amos 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---