Acts_as_taggable gem: deleting tags


#1

I have the following quick-and-dirty hack in my model:

def after_destroy
# search for orphaned tags and delete them
orphans = Resource.tags_count :count => ‘= 0’
orphans.keys.each {|tag| Tags.find_by_name(‘tag’).destroy}
end

It’s nice from a readable code perspective, but it seems inefficient -
is there some way to do this with only one query?

martin


#2

Nevermind, this doesn’t even work - it was masked by another problem
in my code! So how would I go about deleting orphanned tags?

martin


#3

Martin
>So how would I go about deleting orphanned tags?

I added this method to Tag to find orphan tags:

def Tag.find_orphans
Tag.find_by_sql(‘select tags.* from tags left JOIN taggings ON
tags.id = taggings.tag_id where taggings.tag_id is null’)
end

Alain


#4

On 4/27/06, Alain R. removed_email_address@domain.invalid wrote:

Martin
>So how would I go about deleting orphanned tags?

I added this method to Tag to find orphan tags:

def Tag.find_orphans
Tag.find_by_sql(‘select tags.* from tags left JOIN taggings ON
tags.id = taggings.tag_id where taggings.tag_id is null’)
end

Thanks! Looks like I need to go back and relearn SQL properly.

martin