Rails Associations、habtm?多態?両方?
-
05-07-2019 - |
質問
Railsアプリには、Projects、BlogPosts、Imagesの3つのモデルがあります。プロジェクトとBlogPostには多くのリンクされた画像を含めることができ、画像はプロジェクト、BlogPost、またはその両方にリンクできます。
これをRailsで機能させるための関連付けを設定する最良の方法は何ですか?
解決
habtmを別のモデルクラスImageLinkにいじります。その後、次のようになります:
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
所属していません StackOverflow