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.
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2006-04-26 19: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
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2006-04-26 20: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
0091f92762685860109bbcb02edfdf27?d=identicon&s=25 Alain Ravet (Guest)
on 2006-04-26 21: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
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2006-04-26 21:23
(Received via mailing list)
On 4/27/06, Alain Ravet <arav2132@biz.tiscali.be> 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.