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
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
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
On 4/27/06, Alain R. [email protected] 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