acts_as_taggable_on conflicting with after_save callback #147

Closed
ddellacosta opened this Issue Apr 27, 2011 · 2 comments

Comments

Projects
None yet
2 participants

Hey there, this is not critical as there is an easy workaround, but I spent a bit of time pulling my hair out because of this behavior: it seems like if you have an after_save callback in place before the call to acts_as_taggable_on, acts_as_taggable_on won't work via an instance of the model (at least with the default tag_list, which is all I tried).

class Document < ActiveRecord::Base
  after_save :do_something
  acts_as_taggable
  attr_accessible :tag_list
end

irb(main):091:0> @doc.tag_list
=> []
irb(main):092:0> @doc.tag_list = "one, two, three"
=> "one, two, three"
irb(main):093:0> @doc.save!
=> true
irb(main):094:0> @doc.tag_list
=> []
irb(main):095:0> ActsAsTaggableOn::Tag.named("one")
=> []
irb(main):096:0> ActsAsTaggableOn::Tag.named("two")
=> []
irb(main):097:0> ActsAsTaggableOn::Tag.named("three")
=> []
irb(main):098:0> ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("one")
=> #<ActsAsTaggableOn::Tag id: 1, name: "one">
irb(main):099:0> ActsAsTaggableOn::Tag.named("one")
=> [#<ActsAsTaggableOn::Tag id: 1, name: "one">]
irb(main):100:0>

The workaround is simple, just make sure acts_as_taggable_on is called in the model before after_save:

# Works:
class Document < ActiveRecord::Base
  acts_as_taggable
  after_save :do_something
  attr_accessible :tag_list
end

Not sure if this is something that should be fixed or not, I have no idea if it affects other callbacks either, but I'm posting it here in case someone else is having the same issue.

Collaborator

artemk commented Jul 30, 2011

the only problem here is that if your filter do_something return false it will break callback chain and gem's after_save callback won't even run. It is no matter where it is defined. Read more

artemk closed this Jul 30, 2011

Sure, that makes sense—but that's true regardless of whether we're talking about using the callback with acts_as_taggable or not, isn't it? The problem I was having wasn't related to do_something failing. Anyways, I just put this up originally to help someone out in case they were having similar issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment