私は多くのとProjectを持つモデルを持っています。と の両方があります。添付ファイルは多態的です。1 つのプロジェクトのすべての添付ファイルを照会するにはどうすればよいですか?poststaskspoststasksattachments
attachableテーブルがないため、明らかにこれは機能しません。と がpostsありtasksます。また、can't eager load polymorphic association 'attachable'.
# project.rb
def attachments
Attachment.joins(:attachable).where('attachable.project_id = ?', id)
end
コードの残りの部分:
class Project < ActiveRecord::Base
has_many :posts
has_many :tasks
end
class Post < ActiveRecord::Base
belongs_to :project
has_many :attachments, as: :attachable
end
class Task < ActiveRecord::Base
belongs_to :project
has_many :attachments, as: :attachable
end
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
end