# Tag Cloud Code

**URL:** <https://www.ruby-forum.com/t/tag-cloud-code/83933>\
**Category:** Rails\
**Created:** [March 19, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933 "2006-03-19T13:58:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 19, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933/1 "2006-03-19T13:58:18Z")

</div>

Does anyone have any sample code for a tag cloud.  
I’ve looked at typo but they’ve just got complicated sql (which I don’t  
have a clue about). All I want is to select tags and make them  
bigger/smaller depending on the number of pages that are ‘tagged’ by  
them. Thanks in advance

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 19, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933/2 "2006-03-19T14:13:06Z")

</div>

On 3/19/06, Alex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

In the controller:  
@tags = Tag.find(:all,:include =\> :tagged\_objects)

In the view:  
\<% @tags.each do |t| %\>  
\<%= link\_to(tag.name,{}, {:style =\> “font-size: #{(  
tag.tagged\_objects.size+1)\*10}px”}) %\>  
\<% end %\>  
Or something like that.

joey\_\_  
[http://www.feedreed.com](http://www.feedreed.com)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 19, 2006, 2:28pm UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933/3 "2006-03-19T14:28:13Z")

</div>

I’ve posted a ruby-sample-code for tagclouds on my blog, a while ago

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 19, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933/4 "2006-03-19T17:12:05Z")

</div>

i’ll assume he’s the author of  
[http://blog.craz8.com/articles/2005/10/28/](http://blog.craz8.com/articles/2005/10/28/)

Alex wrote:

> tijer wrote:
> 
> > I’ve posted a ruby-sample-code for tagclouds on my blog, a while ago
> 
> What’s the address?

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 20, 2006, 6:45am UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933/5 "2006-03-20T06:45:26Z")

</div>

Would be interested to know what the database structure is for a tag  
cloud? I saw this post:

> **[The best online communities for developers tutorial2.de](https://www.tutorial2.de/best-online-communities/)**
>
> An online tech community is the most exciting place for a software developer to spend their time. It not only offers the chance to work and interact remotely, but also helps in honing one’s own skills and becoming a well-rounded programmer. Whether...

but apparently there may be a different recommended path with RoR? Any  
insights?

VW

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 20, 2006, 7:47am UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933/6 "2006-03-20T07:47:20Z")

</div>

My tags table caches the number of posts they’re associated with as a  
database field, named post\_count. The code I use (in a helper):

def cloud\_view(tags)  
html = “”  
mid = tags.map {|i| i.post\_count}.max / 3.0

tags.each do |tag|  
size = 100 \* tag.post\_count / mid  
size = 80 if size \< 80  
html \<\< link\_to(tag.name, “post/list/#{tag}”, :style =\>  
“font-size: #{size}%”) \<\< " "  
end

return html  
end

This caps the maximum size of a tag to 300%, and the minimum to 80%.

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 19, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/tag-cloud-code/83933/7 "2006-03-19T14:38:14Z")

</div>

tijer wrote:

> I’ve posted a ruby-sample-code for tagclouds on my blog, a while ago

What’s the address?
