Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 784
Dia Kharrat Avatar asked Feb 12 '11 07:02

Dia Kharrat


People also ask

What is the difference between Has_one and Belongs_to?

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.

What is a polymorphic relationship rails?

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.


1 Answers

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.

like image 79
James Chen Avatar answered Sep 29 '22 10:09

James Chen