Hi Experts, Is there way to do Random pagination? I''m not with order, or sort basis. just random pagination regards, Bala --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala wrote:> Hi Experts, > > Is there way to do Random pagination? > > I''m not with order, or sort basis. just random pagination > > regards, > > Balahmm. Here goes my rambling noob attempts: 3 ways - 1) You can get records from the db in random order with :order => ''random()'', but this is a bit inefficient. There''s a plugin called ''random_finders'' which is more efficent, read about it here: http://source.collectiveidea.com/public/rails/plugins/random_finders/ The problem with the pagination system i use (for you) is that it reorders and re-gets the data for each page when it''s required. IE, if i want page 5 (where pages start at 1), and i have 15 items per page, then i do an ordered query and ask for items 60 to 74: return Article.find( :all, :order => "points DESC, added_at DESC", :limit => params[:items_per_page], :offset => params[:offset] ) If you use the above plugin, you can do return Article.find( :all, :order => :random, :limit => params[:items_per_page], :offset => 0 ) To get (eg) 15 random items from the database. But, using this system, every page will be a new random selection, so you might get the same items on page 1, page 2, etc: in fact, the concept of ''pages'' becomes meaningless, you might as well just hit refresh to get another random selection. 2) Alternatively, you can get all the records out of the db and randomize the resulting array with randomized_records = MyClass.find(:all).sort_by{rand} Then, you can just ask for records (eg) 60 to 74 from this array, with first_item = page_num*page_size last_item = first_item + page_size - 1 page_of_records = randomized_records.slice[first_item..last_item] This approach might be horrible for memory though - i don''t know how many records you''re planning on holding but if its millions then this is probably a bad idea. 3) As a third approach, you could make an array of indexes, randomize it, then pull items out of the DB individually to make your pages: indexes = [] MyClass.times do |i| indexes << i end randomized_indexes = indexes.sort_by{rand} Then, get items from the DB one by one: page = [] first_item = page_num*page_size last_item = first_item + page_size - 1 randomized_indexes.slice[first_item..last_item].each do |i| page << MyClass.find_by_index(i) end Having said all of this, there''s probably a neat Rails helper to do all of this :) -- 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 -~----------~----~----~----~------~----~------~--~---
How about page = rand(max_number_of_pages)? -- Long http://MeandmyCity.com/ - Find your way http://edgesoft.ca/blog/read/2 - No-Cookie Session Support plugin ----- Original Message ----- From: "Bala" <mbbala-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Sent: Wednesday, October 03, 2007 2:22 AM Subject: [Rails] Random Pagination> > Hi Experts, > > Is there way to do Random pagination? > > I''m not with order, or sort basis. just random pagination > > regards, > > Bala >--~--~---------~--~----~------------~-------~--~----~ 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 reply Expert, but my problem is very different approach to random pagination. likewise, i''ve home page where i''ve to display some categories randomly with limit of 5 to 6 datas in a page, if oracle doesnt support random function then how we solve this issues? without complications. regards, Bala On Oct 3, 6:57 pm, "Long" <long...-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> wrote:> How about page = rand(max_number_of_pages)? > > -- Longhttp://MeandmyCity.com/- Find your wayhttp://edgesoft.ca/blog/read/2- No-Cookie Session Support plugin > > ----- Original Message ----- > From: "Bala" <mbb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > Sent: Wednesday, October 03, 2007 2:22 AM > Subject: [Rails]RandomPagination > > > Hi Experts, > > > Is there way to doRandompagination? > > > I''m not with order, or sort basis. justrandompagination > > > regards, > > > Bala--~--~---------~--~----~------------~-------~--~----~ 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 4 Oct 2007, at 11:30, Bala wrote:> > Thanks for the reply Expert, > > but my problem is very different approach to random pagination. > > likewise, i''ve home page where i''ve to display some categories > randomly with limit of 5 to 6 datas in a page, > > if oracle doesnt support random function then how we solve this > issues? without complications. >I vaguely recall reading how wikipedia do their ''random article thing'' - each article is assigned a number between 0 and 1 (randomly and uniformly). - to pick a random article generate a random number p and pick the first article whose assigned number is greater than p Fred> regards, > > Bala > > On Oct 3, 6:57 pm, "Long" <long...-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> wrote: >> How about page = rand(max_number_of_pages)? >> >> -- Longhttp://MeandmyCity.com/- Find your wayhttp://edgesoft.ca/ >> blog/read/2- No-Cookie Session Support plugin >> >> ----- Original Message ----- >> From: "Bala" <mbb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> Sent: Wednesday, October 03, 2007 2:22 AM >> Subject: [Rails]RandomPagination >> >>> Hi Experts, >> >>> Is there way to doRandompagination? >> >>> I''m not with order, or sort basis. justrandompagination >> >>> regards, >> >>> Bala > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Totally blocked :( On Oct 4, 5:30 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 4 Oct 2007, at 11:30, Bala wrote: > > > > > Thanks for the reply Expert, > > > but my problem is very different approach to random pagination. > > > likewise, i''ve home page where i''ve to display some categories > > randomly with limit of 5 to 6 datas in a page, > > > if oracle doesnt support random function then how we solve this > > issues? without complications. > > I vaguely recall reading how wikipedia do their ''random article thing'' > - each article is assigned a number between 0 and 1 (randomly and > uniformly). > - to pick a random article generate a random number p and pick the > first article whose assigned number is greater than p > > Fred > > > regards, > > > Bala > > > On Oct 3, 6:57 pm, "Long" <long...-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> wrote: > >> How about page = rand(max_number_of_pages)? > > >> -- Longhttp://MeandmyCity.com/-Find your wayhttp://edgesoft.ca/ > >> blog/read/2- No-Cookie Session Support plugin > > >> ----- Original Message ----- > >> From: "Bala" <mbb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >> To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > >> Sent: Wednesday, October 03, 2007 2:22 AM > >> Subject: [Rails]RandomPagination > > >>> Hi Experts, > > >>> Is there way to doRandompagination? > > >>> I''m not with order, or sort basis. justrandompagination > > >>> regards, > > >>> Bala--~--~---------~--~----~------------~-------~--~----~ 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 4 Oct 2007, at 14:37, Bala wrote:> > Totally blocked :( >Say what? Fred> On Oct 4, 5:30 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 4 Oct 2007, at 11:30, Bala wrote: >> >> >> >>> Thanks for the reply Expert, >> >>> but my problem is very different approach to random pagination. >> >>> likewise, i''ve home page where i''ve to display some categories >>> randomly with limit of 5 to 6 datas in a page, >> >>> if oracle doesnt support random function then how we solve this >>> issues? without complications. >> >> I vaguely recall reading how wikipedia do their ''random article >> thing'' >> - each article is assigned a number between 0 and 1 (randomly and >> uniformly). >> - to pick a random article generate a random number p and pick the >> first article whose assigned number is greater than p >> >> Fred >> >>> regards, >> >>> Bala >> >>> On Oct 3, 6:57 pm, "Long" <long...-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> wrote: >>>> How about page = rand(max_number_of_pages)? >> >>>> -- Longhttp://MeandmyCity.com/-Find your wayhttp://edgesoft.ca/ >>>> blog/read/2- No-Cookie Session Support plugin >> >>>> ----- Original Message ----- >>>> From: "Bala" <mbb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>> To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >>>> Sent: Wednesday, October 03, 2007 2:22 AM >>>> Subject: [Rails]RandomPagination >> >>>>> Hi Experts, >> >>>>> Is there way to doRandompagination? >> >>>>> I''m not with order, or sort basis. justrandompagination >> >>>>> regards, >> >>>>> Bala > > > >--~--~---------~--~----~------------~-------~--~----~ 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 you should do the leg work yourself if you want it done. But here is the pseudo as you seem to need it. 1. count the total number of rows - SQL 2. calculate the max_number_of_pages - Ruby 3. page = rand(max_number_of_pages) - Ruby 4. calculate the offset - Ruby 5. select the rows using ''limit offset, items_per_page'' - SQL If that doesn''t help, I''d have to agree with Frederick. ''Say what?'' -- Long ----- Original Message ----- From: "Bala" <mbbala-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Sent: Thursday, October 04, 2007 5:30 AM Subject: [Rails] Re: Random Pagination> > Thanks for the reply Expert, > > but my problem is very different approach to random pagination. > > likewise, i''ve home page where i''ve to display some categories > randomly with limit of 5 to 6 datas in a page, > > if oracle doesnt support random function then how we solve this > issues? without complications. > > regards, > > Bala > > On Oct 3, 6:57 pm, "Long" <long...-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> wrote: > > How about page = rand(max_number_of_pages)? > > > > -- Longhttp://MeandmyCity.com/- Find your wayhttp://edgesoft.ca/blog/read/2- No-Cookie SessionSupport plugin> > > > ----- Original Message ----- > > From: "Bala" <mbb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > Sent: Wednesday, October 03, 2007 2:22 AM > > Subject: [Rails]RandomPagination > > > > > Hi Experts, > > > > > Is there way to doRandompagination? > > > > > I''m not with order, or sort basis. justrandompagination > > > > > regards, > > > > > Bala > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala wrote:> Hi Experts, > > Is there way to do Random pagination? > > I''m not with order, or sort basis. just random pagination > > regards, > > BalaI''m quite into the idea of pseudo-random stuff. So you could define some transformation on id with a given set of parameters, chuck those parameters into a session, and recreate it. although I havent bothered to think of a function. A silly way could be; session[:random_seed] ||= rand(50) session[:random_direction] ||= ["ASC", "DESC"][rand(2)] Content.find(:all, :order => "SUBSTRING(content.body, #{session[:random_seed]}) #{session[:random_direction]}" ) But that won''t be fast, at all. -- 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 -~----------~----~----~----~------~----~------~--~---
thank you guys, it works well, @categories = Category.find(:all, :conditions => ["parentid=0"], :limit => 5, :offset => rand(5)) remember rand(5) is hardcoded thanks for all your suggestions and help regards, Bala On Oct 4, 10:25 pm, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Bala wrote: > > Hi Experts, > > > Is there way to do Random pagination? > > > I''m not with order, or sort basis. just random pagination > > > regards, > > > Bala > > I''m quite into the idea of pseudo-random stuff. > So you could define some transformation on id > with a given set of parameters, > > chuck those parameters into a session, > and recreate it. > > although I havent bothered to think of a function. > > A silly way could be; > > session[:random_seed] ||= rand(50) > session[:random_direction] ||= ["ASC", "DESC"][rand(2)] > Content.find(:all, > :order => "SUBSTRING(content.body, #{session[:random_seed]}) > #{session[:random_direction]}" > ) > > But that won''t be fast, at all. > -- > 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 -~----------~----~----~----~------~----~------~--~---