ruby-on-railsoverridinghas-and-belongs-to-manysetteralias-method-chain

alias_method_chain on an HABTM attribute's setter isn't working


So I have a HABTM for both Posts and Topics. A Post HABTM Topics, and a Topic HABTM Posts. What I need to do is call some method in conjunction with calling post.topics=()

This is what I've tried doing in Post.rb:

def topics_with_extra_stuff=(topics)
  topics_without_extra_stuff=(topics)
  extra_stuff()
end
alias_method_chain :topics=, :extra_stuff

However, this now breaks post.topics=()

I won't get an error or anything, but topics() will still be the old value after changing it with topics=()

If I raise an error in topics_with_extra_stuff=, the trace will say that there was an error in topics=, so I know it's getting in there. I also know that extra_stuff() was called.

Here's an example of the output:

>> p = Post.last
=> #<Post id:1 ....>
>> p.topics
=> [#<Topic id:1 ....>, #<Topic id:2 ....>]
>> p.topics = [ p.topics.first ]
=> [#<Topic id:1 ....>]
>> p.topics
=> [#<Topic id:1 ....>, #<Topic id:2 ....>]

It shouldn't still have 2 Topics, just 1.

Thanks for any insight.


Solution

  • I ended up just using the association callback :before_add instead.