Rails Associations, habtm? Polymorphic? Both?

In my Rails application, I have three models: Projects, BlogPosts, and Images. Projects and BlogPost can have many related images, and the image can be associated with Project, BlogPost, or both.

What is the best way to create associations for working in Rails?

+5
source share
1 answer

I would call habtm a separate ImageLink model class. Then you will get:

Project
  has_many :image_links, :as => :resource
BlogPost
  has_many :image_links, :as => :resource
ImageLink
  belongs_to :image
  belongs_to :resource, :polymorphic => true
Image:
  has_many :image_links
+9
source

All Articles