I''m getting ready to write some generic code to handle "rotation" of content on our website, an online community featuring authors, books, etc. What the "rotation" means is that content such as Featured Blogs, Featured Author, Featured Book, and so on, needs to rotate through a bunch (e.g. 10+) of DB records, so every time you do a browser refresh/reload, you see something different and that way each author, book, blog, etc. gets featured on the website. How would you handle that? Use cookies to see what the user saw last? Have the content cached on the server somehow and loop through that? On an old Java-based community I build, I used a static object on the web server consisting of an array of content records and an array index that I incremented for each access to the content; then, I simply looped through the array for each access till I hit the end, at which point I reset the array index to 0 in order to start the array from the beginning on the next access. Thanks in advance. -- 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 -~----------~----~----~----~------~----~------~--~---
Peter De Berdt
2007-Jul-26 13:44 UTC
Re: How do I implement "rotating" content on a website?
On 26 Jul 2007, at 15:06, Ben Knight wrote:> I''m getting ready to write some generic code to handle "rotation" of > content on our website, an online community featuring authors, books, > etc. What the "rotation" means is that content such as Featured > Blogs, > Featured Author, Featured Book, and so on, needs to rotate through a > bunch (e.g. 10+) of DB records, so every time you do a browser > refresh/reload, you see something different and that way each author, > book, blog, etc. gets featured on the website. > > How would you handle that? Use cookies to see what the user saw last? > Have the content cached on the server somehow and loop through that?If highlighting a certain item on the front page can be random, I would suggest a much easier solution (assuming your using MySQL, don''t know if the other DBMS support this): @featured_blog = Blog.find(:first, :order => "RAND()") @featured_book = Book.find(:first, :order => "RAND()") Add conditions if you want to limit the number of records, e.g. @featured_blog = Blog.find(:first, :conditions => ["updated_at > ?", Date.today.last_month], :order => "RAND()") This basically translates to "SELECT * FROM blogs LIMIT 1 ORDER BY RAND()", it tells MySQL to fetch 1 single random record out of the resultset. Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ 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, Peter. This was a great solution! I''ve already implemented this as an interim solution for the next couple of months since our site hasn''t officially launched and it''ll be a couple/few months till we begin getting a lot of traffic. In the long run, I would like to give everyone precisely equal exposure. Also, I wonder if there is a better solution for faster performance using memory versus database calls. But again, for now, thanks a ton for your help! I have all the rotations working for now and it was very easy to implement since I simply had to add the ":order => "RAND()" stuff since I was already making :find(:first) calls. Peter De Berdt wrote:> On 26 Jul 2007, at 15:06, Ben Knight wrote: > >> Have the content cached on the server somehow and loop through that? > If highlighting a certain item on the front page can be random, I > would suggest a much easier solution (assuming your using MySQL, > don''t know if the other DBMS support this): > > @featured_blog = Blog.find(:first, :order => "RAND()") > @featured_book = Book.find(:first, :order => "RAND()") > > Add conditions if you want to limit the number of records, e.g. > @featured_blog = Blog.find(:first, :conditions => ["updated_at > ?", > Date.today.last_month], :order => "RAND()") > > This basically translates to "SELECT * FROM blogs LIMIT 1 ORDER BY > RAND()", it tells MySQL to fetch 1 single random record out of the > resultset. > > > Best regards > > Peter De Berdt-- 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2007-Jul-26 16:49 UTC
Re: How do I implement "rotating" content on a website?
> > Hi, Peter. This was a great solution! I''ve already implemented this as > an interim solution for the next couple of months since our site hasn''t > officially launched and it''ll be a couple/few months till we begin > getting a lot of traffic. > > In the long run, I would like to give everyone precisely equal exposure. > Also, I wonder if there is a better solution for faster performance > using memory versus database calls. > > But again, for now, thanks a ton for your help! I have all the > rotations working for now and it was very easy to implement since I > simply had to add the ":order => "RAND()" stuff since I was already > making :find(:first) calls.Be careful using RAND() if your dataset is large. You don''t want to randomly order a million rows only to retrieve one of them. By doing the above you''re forcing the database to consider every row in the table every time. If you have a lot of rows you may want to do this - get total row count from db - in ruby get a random number from 0 to the total row count - fetch a row ordered by a primary key limit 1 offset that random number. While two queries, it will lower the impact on the database a lot. anyway, something to keep in mind in case your data gets big.> > > > > Peter De Berdt wrote: >> On 26 Jul 2007, at 15:06, Ben Knight wrote: >> >>> Have the content cached on the server somehow and loop through that? >> If highlighting a certain item on the front page can be random, I >> would suggest a much easier solution (assuming your using MySQL, >> don''t know if the other DBMS support this): >> >> @featured_blog = Blog.find(:first, :order => "RAND()") >> @featured_book = Book.find(:first, :order => "RAND()") >> >> Add conditions if you want to limit the number of records, e.g. >> @featured_blog = Blog.find(:first, :conditions => ["updated_at > ?", >> Date.today.last_month], :order => "RAND()") >> >> This basically translates to "SELECT * FROM blogs LIMIT 1 ORDER BY >> RAND()", it tells MySQL to fetch 1 single random record out of the >> resultset. >> >> >> Best regards >> >> Peter De Berdt > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Thanks, Philip. Another friend of mine also suggested the following solution. Any thoughts on this solution? ---- The Rand will work for now. but you could get duplicates, etc. especially with a small table. If your content tables implement acts_as_list, they''ll have a sequence number you can use to select. The current sequence number can be kept in a cookie. You can treat your content records as a circular queue, rolling over when they get to the end. You''ll probably want to cache the max_sequence_number somewhere so you know when to roll back to the beginning without querying the database for the max. Maybe in a cookie or in a custom count field. see http://railscasts.com/episodes/23 The custom count field is better than a cookie if you will be adding/removing content frequently. Also, for an equitable distribution, you probably want to have a visitor start at a random number within the sequence. That way the displays aren''t over-weighted to the beginning of the sequence if a typical visitor only does a few refreshes. ---- -- 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 -~----------~----~----~----~------~----~------~--~---