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?
They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.
Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With