Topic: Multiple Models using acts_as_taggable

Hello,

I am having trouble getting acts_as_taggable to work with multiple models.  Following a rails recipe book I got acts_as_taggable to work with 1 model, but I am struggling with multiple models.

I have acts_as_taggble listed in each model

I would like to be able to query my tag and show my articles and my jobs related to that tag on the same page but separated in different html tables with different link_to depending on tagging_type.

Here is the code I am trying to get to work.  I know I am doing something wrong, but I am not sure what it is.  Thank you.

Controller

class MainController < ApplicationController

.....

  def list
    @tag = Tag.find_by_name(params[:id])

    @jobs =
      if tag_name = params[:id]
        Tag.find_by_name(tag_name).tagged
      else
        @tag = Tag.find_by_name("All Jobs")
        @jobs = Job.find(:all)
      end

    @articles =
      if tag_name = params[:id]
        Tag.find_by_name(tag_name).tagged
      else
        @articles = Articles.find(:all)
      end

.....

  end


VIEW
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=650 ALIGN=CENTER>
<tr>
<td><h1><%=@tag.name%></h1></td>
</tr>

<%for article in @articles -%>
<TR class="<%= cycle('list-line-odd', 'list-line-even')%>">
<TD VALIGN=MIDDLE colspan="3">
     <%=link_to article.title, :action => 'article', :id => article.id %>
</td>
</tr>
<tr>
<TD VALIGN=MIDDLE colspan="3">
    <FONT SIZE=2><i><%=article.title%></i></font>
</td>
</tr>
<%end%>


<%for job in @jobs -%>
<TR class="<%= cycle('list-line-odd', 'list-line-even')%>">
<TD VALIGN=MIDDLE colspan="3">
     <%=link_to job.title, :action => 'job', :id => job.id %>
</td>
</tr>
<tr>
<TD VALIGN=MIDDLE colspan="3">
    <FONT SIZE=2><i><%=job.title%></i></font>
</td>
</tr>
<%end%>

</TABLE>

Re: Multiple Models using acts_as_taggable

funny the last time I tried to install this plugin it failed. I would highly recommend the acts_as_taggable_on_steroids. as the original acts as taggalbe is a bit stale.

I cant test this but you should be able to call the tags per model as memory serves. or perhaps thats just the steriods one.

@jobs.tags.find_by_name('lalaland')

--

Re: Multiple Models using acts_as_taggable

I am able to get my @ variable to collect all the info I need and I can display it in a view, but I am having trouble extracting what is the taggable_type for each item.  I have tried using the Tagging.tagged_class method but I keep getting an error (undefined method 'base_class' for Array:Class).  I think I can get it to work for 1 tag but not mulitple tags.  Is there another way?

Tagging.tagged_class(@tags)

Thank you for any help.

Re: Multiple Models using acts_as_taggable

I figured out my foolish mistake.  I was trying to use '@tags' and should be been using just 'tag' as I looped through the @tags collection.

DOES NOT WORK

Tagging.tagged_class(@tags)

DOES WORK
Tagging.tagged_class(tag)