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 On 4/26/06, Martin DeMello <martindemello@gmail.com> wrote:> 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 >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 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'') > endThanks! Looks like I need to go back and relearn SQL properly. martin