While I was viewing the Video 2 in Intermediate Rails, I noticed that Matthew implemented Polymorphism in a strange way. He made the Polymorphic Relationship on the Shouts Model
but he never actively declared the other side of the relationship on the different shouts. Yet
some how the Different Shouts was still able to relate to the Shouts. The code that I have based
on the video towards the end of Video 2 looks like this:
class Shout < ActiveRecord::Base
belongs_to :user
belongs_to :content, polymorphic: true
default_scope {order("created_at DESC")}
end
and the corresponding classes looks like:
class TextShout < ActiveRecord::Base
end
class PhotoShout < ActiveRecord::Base
has_attached_file :image, styles: { shout: "200x200>"}
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end
Has anybody else noticed that this works? I Know we used the relationship inside the text shout controller and in the photo shouts controller. But I still don’t get why that should work based on the way these models are formed. Any Ideas?