I'm trying to use Wisper with Rails 4 / AR and encountering an issue.
Let's say I have Email and Creep AR models, and I want the Creep to know when a reply was received (Creep is not necessarily the sender...)
So I do:
email = Email.create! params
creep = Creep.last
email.subscribe(creep, on: :reply_received, with: :success)
and if immediately do:
email.publish :reply_received
It will work (Creep instances have a success method).
However, if i later do:
email = Email.find(id)
or:
email = Email.last
The event is not broadcast. I'm guessing this is because the new email is a different instance, and thus has no listeners subscribed. What am I doing wrong?
You are correct. Using
email.subscribeyou are subscribing the listener to a single object. When you doEmail.findyou get back a differentemailobject (checkemail.object_id).You can either subscribe
creepto the newly returned object:Or you could subscribe
creepto all instances ofEmaillike this:You'll proberbly want to do the above in an initializer so it only happens once.
However this might be an issue because it seems you want to subscribe
Creep.lastwhich will change over time. In which case you can do something fancy like this: