Hello All, I have to choose 6 records from the table but randomly. I have written the code: @products = Product.find(:all, :conditions => "feature = 1", :limit => 6) But it fetches only the first six records from the table. I want to choose only 6 products but in random order not the first six. Please tell me how to implement it. Any help would be greatly appreciated. Thanks, Rohit. -- 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 -~----------~----~----~----~------~----~------~--~---
Get all the product ids: ids = Product.find(:all, :select => :id).map(&:id) Choose six at random" random_ids = ... # left as an exercise for the reader @products = Product.find(random_ids) --~--~---------~--~----~------------~-------~--~----~ 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 Dec 4, 6:58 am, kevin cline <kevin.cl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Get all the product ids: > ids = Product.find(:all, :select => :id).map(&:id) > > Choose six at random" > random_ids = ... # left as an exercise for the readerThat will be incredibly inefficient. ''Select * from products order by rand() limit 6'' is also not massively fast. One fast way of doing this (which happens to be the way wikipedia does their random article search): assign to every product a random number between 0 and 1 When you want to get some random products, then pick a random number and select the products whose random value is great than the number you''ve just picked. If you''ve got an index on the column, this should be pretty speedy. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Fred, Sorry....am not able to understand what you have written. How to assign a random number between 0 to 1 to a product? Could you please explain? Thanks, Rohit. -- 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 Dec 4, 2007 9:18 AM, Rohit Mehra <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi Fred, > > Sorry....am not able to understand what you have written. > How to assign a random number between 0 to 1 to a product? > Could you please explain? >Well you have a column on the products table that contains a random number. Fred --~--~---------~--~----~------------~-------~--~----~ 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 use custom SQL SELECT ... FROM my_table ORDER BY RAND() LIMIT (N) On Dec 4, 6:43 am, Rohit Mehra <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello All, > > I have to choose 6 records from the table but randomly. > I have written the code: > @products = Product.find(:all, :conditions => "feature = 1", :limit => > 6) > But it fetches only the first six records from the table. I want to > choose only 6 products but in random order not the first six. > > Please tell me how to implement it. > Any help would be greatly appreciated. > > Thanks, > Rohit. > -- > 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 -~----------~----~----~----~------~----~------~--~---
He Fred, that''s a slick idea. I was mulling over the best way to do this -- in the course of programming an online game, I needed to pick n contestants from a pool of arbitrary size, then narrow that to n/2 contestants, then n/4, finally ending up with just one. Adding and indexing a random number beforehand sounds like a good way to go. Thanks for the thought! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Really cool ... On Dec 4, 4:59 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 4, 6:58 am, kevin cline <kevin.cl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Get all the product ids: > > ids = Product.find(:all, :select => :id).map(&:id) > > > Choose six at random" > > random_ids = ... # left as an exercise for the reader > > That will be incredibly inefficient. ''Select * from products order by > rand() limit 6'' is also not massively fast. > > One fast way of doing this (which happens to be the way wikipedia does > their random article search): > assign to every product a random number between 0 and 1 > > When you want to get some random products, then pick a random number > and select the products whose random value is great than the number > you''ve just picked. If you''ve got an index on the column, this should > be pretty speedy. > > Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---