Acts_As_Taggable Plugin multiple controllers

I have Acts_As_Taggable Plugin working. I have a HR controller and a
sales controller.
I have a document in hr tagged whitepaper and a differnent document in
sales tagged whitepaper
when I am in hr I see the hr document tagged with whitepaper and not
the sale document (what I want). but if I click on the tag whitepaper
I return two documents, hr and sales. I just want to return the hr
document because the sales document is for sales people only.

I’m guessing I need to filter it somewhere but haven’t found the place
yet.

taggings db
create_table :taggings do |t|
t.column :tag_id, :integer
#id of tagged object
t.column :taggable_id, :integer
#type of object tagged
t.column :taggable_type, :string
end

thanks in advance
john

Hi,

On Fri, 2006-14-07 at 09:44 -0500, John I. wrote:

I have Acts_As_Taggable Plugin working. I have a HR controller and a
sales controller.
I have a document in hr tagged whitepaper and a differnent document in
sales tagged whitepaper
when I am in hr I see the hr document tagged with whitepaper and not
the sale document (what I want). but if I click on the tag whitepaper
I return two documents, hr and sales. I just want to return the hr
document because the sales document is for sales people only.

I’m guessing I need to filter it somewhere but haven’t found the place yet.

I needed to do something similar. I used the “with_scope” method to do
the following (using your example). In my controller where I have the
method doing the find_tagged_with, I do this:

Document.with_scope( :find =>
{ :conditions => [“dept_id = ?”, department] }) do
@docs = Document.find_tagged_with(…)
end

Any find_* that happen within the “with_scope” block will be constrained
with the conditions specified.

Rick

this is what happens when a newbie tries things he doesn’t fully
understand, and I thought not touching a computer over the weekend
would clear my head. it didn’t

in the line
{ :conditions => [“dept_id = ?”, department] }) do
i would either have HR of SALES?
If so me select doesn’t work. I get this error.

Mysql::Error: Unknown column ‘dept_id’ in ‘where clause’: SELECT *
FROM tags WHERE (dept_id = ‘hr’) AND (tags.name = ‘jim’ ) LIMIT 1

For some reason I’m thinking I need to filter on the taggalbe_type
column in the taggings table. but if I try that a get and error also.
unrecinizable table…

Thanks
john.

Hi again,

On Mon, 2006-17-07 at 08:44 -0700, Rick T. wrote:

You may be able to do something like this (although it doesn’t quite
feel right to me for some reason … maybe it’s just that there’s too
much knowledge about the implementation of tagging … not sure). I
haven’t tried this, so I don’t know if it will work.

Taggings.with_scope( :find =>
{ :conditions => [“taggable_type = ‘HR’”] }) do

@docs = Document.find_tagged_with(…)
end

Looks like the above doesn’t work. And, in fact, the original method
that I’d posted doesn’t work either. (I thought it was … I have bad
test case)

It appears that the “with_scope” method on a model is not honoured by
the find_by_sql method, which is what the find_tagged_with method makes
use of.

I’d guess that find_by_sql is not meant to honour anything defined by
“with_scope” since find_by_sql is the method used to just run good 'ol
SQL .

So, I’d guess that you’d have to write your own
find_tagged_with_and_scoped_by method.

Rick

answer…

i origianlly had this in the controller…

@hrfiles = if tag_name = params[:id]
  #Tag.find_by_name(tag_name).tagged
  else
    hrfile.find(:all)
  end

This works

@hrfiles = if tag_name = params[:id]
  hrfile.find_tagged_with(tag_name)
  else
    hrfile.find(:all)
  end

Hi,

On Mon, 2006-17-07 at 09:04 -0500, John I. wrote:

this is what happens when a newbie tries things he doesn’t fully
understand, and I thought not touching a computer over the weekend
would clear my head. it didn’t

:slight_smile:

in the line
{ :conditions => [“dept_id = ?”, department] }) do
i would either have HR of SALES?
If so me select doesn’t work. I get this error.

Mysql::Error: Unknown column ‘dept_id’ in ‘where clause’: SELECT *
FROM tags WHERE (dept_id = ‘hr’) AND (tags.name = ‘jim’ ) LIMIT 1

Sorry, I’d made some assumptions about the structure of your database.
I’d assumed that there was a Document and Dept model and that there was
a has_many relationship between Dept and Document. (ie. Dept has_many
Document and Document belongs_to Dept). That’s reflected in the above
“:conditions” line. That’s where the “dept_id” column would come from.

For some reason I’m thinking I need to filter on the taggalbe_type
column in the taggings table. but if I try that a get and error also.
unrecinizable table…

You may be able to do something like this (although it doesn’t quite
feel right to me for some reason … maybe it’s just that there’s too
much knowledge about the implementation of tagging … not sure). I
haven’t tried this, so I don’t know if it will work.

Taggings.with_scope( :find =>
{ :conditions => [“taggable_type = ‘HR’”] }) do

@docs = Document.find_tagged_with(…)
end

Rick

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs