Bob Sanders
2008-May-12 07:29 UTC
How would you program "You have posted 5 days in a row..."?
If you want to show the number of consecutive days a user has posted (e.g. "You have posted 5 days in a row"), how would you do it? I have no idea how to do this. I''m guessing it''s something along the lines of current_user.posts.find(:all, [SOMETHING HERE]).size Any idea? -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Bush
2008-May-12 08:21 UTC
Re: How would you program "You have posted 5 days in a row..
Bob Sanders wrote:> If you want to show the number of consecutive days a user has posted > (e.g. "You have posted 5 days in a row"), how would you do it?This is tricky. What you''re after is the number of days since the last day the user didn''t post anything. That is, the maximum date before today that does NOT appear for that user. I can''t think of a way offhand you could get this in one hit. One solution is to keep the count in the model. Then, each time a user posts, find the last post date for them and if it was yesterday, update the count, else reset it. -- 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 -~----------~----~----~----~------~----~------~--~---
Greg Donald
2008-May-13 03:36 UTC
Re: How would you program "You have posted 5 days in a row..."?
On 5/12/08, Bob Sanders <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> If you want to show the number of consecutive days a user has posted > (e.g. "You have posted 5 days in a row"), how would you do it? > > I have no idea how to do this. I''m guessing it''s something along the > lines of current_user.posts.find(:all, [SOMETHING HERE]).sizeMaybe something like: Post.count( :conditions => (1..5).collect { |x| "created_at LIKE ''#{ ( Date.today - x ).to_s }%''" }.join( '' AND '' ) ) -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---
Bob Sanders
2008-May-13 04:39 UTC
Re: How would you program "You have posted 5 days in a row..
Mark Bush wrote:> Bob Sanders wrote: >> If you want to show the number of consecutive days a user has posted >> (e.g. "You have posted 5 days in a row"), how would you do it? > > This is tricky. What you''re after is the number of days since the last > day the user didn''t post anything. That is, the maximum date before > today that does NOT appear for that user. I can''t think of a way > offhand you could get this in one hit. > > One solution is to keep the count in the model. Then, each time a user > posts, find the last post date for them and if it was yesterday, update > the count, else reset it.That''s a great idea, Mark. I didn''t think of keeping the count in the model. That''s definitely one way to do it. -- 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 -~----------~----~----~----~------~----~------~--~---
Bob Sanders
2008-May-13 04:42 UTC
Re: How would you program "You have posted 5 days in a row..
Greg Donald wrote:> On 5/12/08, Bob Sanders <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> If you want to show the number of consecutive days a user has posted >> (e.g. "You have posted 5 days in a row"), how would you do it? >> >> I have no idea how to do this. I''m guessing it''s something along the >> lines of current_user.posts.find(:all, [SOMETHING HERE]).size > > Maybe something like: > > Post.count( :conditions => (1..5).collect { |x| "created_at LIKE ''#{ ( > Date.today - x ).to_s }%''" }.join( '' AND '' ) ) > > > -- > Greg Donald > http://destiney.com/Thanks for the idea, Mark. I''m going to test that out when I get back to the company. Thank you so much for the tip. -- 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 -~----------~----~----~----~------~----~------~--~---