# Count the Split up Tag values

**URL:** <https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903>\
**Category:** Rails\
**Created:** [July 1, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903 "2008-07-01T16:54:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [July 1, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903/1 "2008-07-01T16:54:20Z")

</div>

Hello!

I have in my database the tags for a specific entry separated by  
commas and I’m using a SQL statement that pulls all the tags for a  
bunch of entries and the ruby code on the page:

\<%- for notes in @tags\_sql do -%\>  
\<% notes.tag.split(", ").uniq.each do |tags| %\>

- \<%= link\_to(tags, {:tag =\> tags, :controller =\> 'project', :action =\> 'tags', :id =\> @project\_id}) %\>
 \<% end %\>

It’s able to split after each ", " so I have each tags listed in the  
unordered list but I also need to have next each individual tag the  
count of how many times that tag shows up across the sql statement.

Is there anyway to do that in my code? I know I can’t do it in my SQL  
statement because it counts the entire chunk per entry like “a, b, c,  
d” instead of a b c d - it gets broken up in that code I put above.

Basically if its “a, c, a, d” I got it so far to be

a  
c  
d

BUT! I need it to be

a (2)  
c (0)  
d (0)

Any help would be appreciated. You guys are really awesome.

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [July 1, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903/2 "2008-07-01T17:07:14Z")

</div>

How about Note has\_many :tags so that rails takes care of it all

if you dont like that (which i strongly recommend having implemented  
your solution and replacing it with the association)

you will need to write your own method that counts the instances of an  
object in an array. it comes by default in ruby 1.9, unfortunatley,  
rails does not work with it.

def array\_count arr, obj  
count = 0  
arr.each do |item|  
count += 1 if item == obj  
end  
count  
end

more or less this should work if you put it in your helper so that it  
doesnt clobber the controller, or even still in the model

=== note.rb

class Note \< AR::Base  
has\_many :tags # this would be ideal

def note\_count tag  
count = 0  
notes.each do |note|  
count += 1 if note.tags.include?(tag)  
end  
count  
end  
end

hope it helps

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [July 1, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903/3 "2008-07-01T17:54:52Z")

</div>

I answer here coz i forgot your code already…

=== routes.rb

map.resources :tags

=== controllers/tags\_controller.rb

def index  
@tags = Tag.find(:all)  
end

=== views/tags/index.rhtml

\<%- for tag in @tags do -%\>  
\<%= link\_to tag, :controller =\> ‘tags’, :action =\> ‘show’, :id =\>  
tag %\> ( \<%= tag.notes.size %\> )  
\<% end %\>

This will display what you want  
tag1 (3)  
tag2 (0)  
tag3 (6)

where the number is the number of notes associated with that tag.

the link stuff ignore if you know where and what to call, but id  
suppose when you click on one of them it shows you with more depth,  
what is in the tag

=== models/tag.rb

has\_and\_belongs\_to\_many :notes

=== models/note.rb

has\_and\_belongs\_to\_many :tags

=== cmd

ruby script/generate migration add\_join\_table

create\_table :tags\_notes, :id =\> false do |t|  
t.integer :tag\_id  
t.integer :note\_id  
end

You really need to go through some tutorials, people are not gonna  
solve these types of issues so in depth all the time…

but hope it helps

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [July 1, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903/4 "2008-07-01T17:57:48Z")

</div>

I tried:

 \<%- for notes in @tags\_sql do -%\> \<% count = 0 notes.tag.split(", ").each do |tags| count += 1 if notes.tag.split(", ").include?(tags)%\> - \<%= link\_to(tags, {:tag =\> tags, :controller =\> 'project', :action =\> 'tags', :id =\> @project\_id}) %\> \<%= count%\>
 \<% end %\> 

But it just counts each one like. I know I’m totally doing it work.

a 1  
b 2  
c 3

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [July 1, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903/5 "2008-07-01T22:40:31Z")

</div>

Marra,  
There are lots of things that tie up a function in a rails project.  
(function being the tag functionality) the solution i gave is based on  
your note model having many tags and vice-versa. If you want to follow  
the “list in one column” approach, the solution id give you is the  
same one i posted before, because its just neater, and follows the  
rails conventions. So a couple of suggestions:

1. Read blogs, tutorials and screen casts because these questions will  
all be answered there
2. Follow rails conventions, this is a tough one to learn because  
there is nothing that protects you from doing things your way, the  
flipside is that when you want to use your code into some pre-  
established rails “way” it generally doesnt work and you have to spend  
time re diong what you did to fit to the rails conventions.
3. Google before you post. Your question was something that i have  
though of doing a coupe of times and i wanted others to see why is it  
not a good idea and how it could be done neatly, in general nobody  
will answer these

good luck

j

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [July 1, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903/6 "2008-07-01T22:42:16Z")

</div>

uuu. one more thing.

never ever ever put login in your views.

if you want to do

count = 0  
notes.tag.split(", “).each do |tags|  
count += 1 if notes.tag.split(”, ").include?(tags)

business, do it in the controller, and pass to the views a variable  
like @tag\_names. (something in the lines of @tags = {:a =\> 2, :b =\>  
2…})

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [July 14, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/count-the-split-up-tag-values/141903/7 "2008-07-14T16:57:39Z")

</div>

Wolas!

I appreciate your help. I was able to find a solution to my problem  
through my own means, but thank you for trying.

I do read a lot of tutorials and blogs, and the ones I found didn’t  
help with my particular problem because of the way I had been working  
already. This is my first Ruby on Rails project, naturally you can  
imagine it won’t be as perfect as it should be, but I am trying my  
best and the project is moving forward.

At any rate I was able to solve my problem on my own - thanks anyway!

Marra
