Forum: Ruby on Rails acts_as_taggable gem: deleting tags

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Martin DeMello (Guest)
on 2006-04-26 21:31
(Received via mailing list)
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
Martin DeMello (Guest)
on 2006-04-26 22:33
(Received via mailing list)
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
Alain R. (Guest)
on 2006-04-26 23:13
(Received via mailing list)
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
Martin DeMello (Guest)
on 2006-04-26 23:23
(Received via mailing list)
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
This topic is locked and can not be replied to.