概要
ポリモーフィック関連で子と親を含めて絞り込むときに、直接SQLを書いています。
必要なデータは取得できていますが、コードが悠長になってしまい、困っています。
もっとrailsっぽく書けないものか、アドバイスをいただけると助かります。
ポリモーフィック関連
class Picture < ApplicationRecord belongs_to :imageable, polymorphic: true end class Employee < ApplicationRecord has_many :pictures, as: :imageable end class Product < ApplicationRecord has_many :pictures, as: :imageable end
子から親のデータも含めて、whereで絞り込み
Employee.joins("JOIN pictures on pictures.imageable_id = employees.id AND pictures.imageable_type = 'Employee' ").where(pictures: { status: 'free' })
あなたの回答
tips
プレビュー