# Count number of downloads and link\_to

**URL:** <https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461>\
**Category:** Rails\
**Created:** [July 18, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461 "2008-07-18T21:39:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [July 18, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461/1 "2008-07-18T21:39:06Z")

</div>

Hello,

I have a download link on my page, whick looks like that :

\<%=link\_to “Download”, @photo.public\_filename, :class =\> “thickbox”%\>  
As you see, I m displyaing an image in a thickbox popup. However I  
would like to count the number of times the user clicks on this link,  
to get a number of downloads, and so increment a value in my db each  
time the link is clicked. But how can I achieve this. Is there a way  
to couple an ajax call to this link ?

Also, I want to display the number of downloads for " today ". Anyone  
has an idea of how I can do this.

Thx in advance,

Regards

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [July 18, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461/2 "2008-07-18T21:45:32Z")

</div>

Hmmm, off the top of my head I would say add two fields to the photo  
model, one ‘download\_count\_total’, and ‘download\_count\_today’. Then  
write a helper function link\_to\_with\_count that would look something  
like:

def link\_to\_with\_count(name, model, options)  
model.update\_attributes(:download\_count\_total =\>  
model.download\_count\_total.next,  
:download\_count\_today =\> model.download\_count\_total.next)  
link\_to name, model, options  
end

Then, you would need to write a script that runs every night at  
midnight and resets every photos download\_count\_today attribute.

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [July 18, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461/3 "2008-07-18T22:03:41Z")

</div>

What’s the last line code used to ?

" link\_to name, model, options "

Thx,

Joel

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [July 18, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461/4 "2008-07-18T23:04:42Z")

</div>

“named\_route :todays\_downloads, {:conditions =\> {:created\_at =\>  
Date.today}}” should just be  
“named\_route :todays\_downloads, :conditions =\> {:created\_at =\>  
Date.today}”. (Where the heck’s the edit button?)

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [July 18, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461/5 "2008-07-18T22:59:26Z")

</div>

First I’d create a column in the table that’s storing your photos  
named “download\_count”. Then add a line to your routes file, something  
like “yourcontroller/update\_download\_count/:id/:count”, then create an  
action in the controller:

def update\_download\_count  
YourModel.update\_attribute :download\_count, params[:count] + 1  
end

and finally, change your link to:  
\<%=link\_to\_remote “Download”, @photo.public\_filename, :url =\> {:action  
=\> update\_download\_count, :id =\> @photo.id, :count =\>  
@photo.download\_count}, :html =\> {:class =\> “thickbox”} %\>

And to display today’s downloads, add a named route to your model:  
named\_route :todays\_downloads, {:conditions =\> {:created\_at =\>  
Date.today}}

Just something quick I thought up, it should help you get on the right  
track 🙂

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [July 19, 2008, 12:51am UTC](https://www.ruby-forum.com/t/count-number-of-downloads-and-link-to/143461/6 "2008-07-19T00:51:28Z")

</div>

Sorry, named\_route should be named\_scope, and the outer { } should be  
removed from it.
