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?

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.

On 5/12/08, Bob S. [email protected] 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/

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.

Greg D. wrote:

On 5/12/08, Bob S. [email protected] 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/

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs