Re: Count the Split up Tag values

\"Wolas!\"
2008-07-01T15:54:15Z

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

On Jul 1, 4:06 pm, "\"Wolas!\""
<jcpen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> 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
> On Jul 1, 3:53 pm, Marra
<bendingunitn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > 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 tosplitafter each ", " so I have each
tags listed in the
> > unordered list but I also need to have next each individual tag the
> >countof 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.