# How would you program "You have posted 5 days in a row..."?

**URL:** <https://www.ruby-forum.com/t/how-would-you-program-you-have-posted-5-days-in-a-row/137349>\
**Category:** Rails\
**Created:** [May 12, 2008, 9:29am UTC](https://www.ruby-forum.com/t/how-would-you-program-you-have-posted-5-days-in-a-row/137349 "2008-05-12T09:29:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 12, 2008, 9:29am UTC](https://www.ruby-forum.com/t/how-would-you-program-you-have-posted-5-days-in-a-row/137349/1 "2008-05-12T09:29:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 12, 2008, 10:21am UTC](https://www.ruby-forum.com/t/how-would-you-program-you-have-posted-5-days-in-a-row/137349/2 "2008-05-12T10:21:00Z")

</div>

Bob S. 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.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 13, 2008, 5:36am UTC](https://www.ruby-forum.com/t/how-would-you-program-you-have-posted-5-days-in-a-row/137349/3 "2008-05-13T05:36:51Z")

</div>

On 5/12/08, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 13, 2008, 6:39am UTC](https://www.ruby-forum.com/t/how-would-you-program-you-have-posted-5-days-in-a-row/137349/4 "2008-05-13T06:39:43Z")

</div>

Mark B. wrote:

> Bob S. 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.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 13, 2008, 6:42am UTC](https://www.ruby-forum.com/t/how-would-you-program-you-have-posted-5-days-in-a-row/137349/5 "2008-05-13T06:42:15Z")

</div>

Greg D. wrote:

> On 5/12/08, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 D.  
> [http://destiney.com/](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.
