Can "touch" be used on a belongs_to polymorphic relationship in Rails?
Asked Answered
H

1

6

I expected the following to work:

class Attachment < ActiveRecord::Base
   belongs_to :attachable, :polymorphic => true, :touch => true
end

which I expect the associated objects to be "touched" when the Attachment record is saved or destroyed. It didn't work. Any ideas why?

Hecht answered 12/2, 2011 at 7:46 Comment(0)
C
9

Yes this should work. I have used this on several projects (2.3.x and 3.0.x) and it just works.

You may try to call touch manually like this: attachment.attachable.touch, then reload the attachable object and see if its updated_at field has been modified. If so, the :touch option should does that automatically.

Chesterton answered 12/2, 2011 at 8:10 Comment(1)
You're right; it's working for me now; I think it wasn't working for me because I forgot to set attr_accessible on the polymorphic relationship.Hecht

© 2022 - 2024 — McMap. All rights reserved.