■モデルに関して class Post < ActiveRecord::Base has_one :video has_one :image end class Video < ActiveRecord::Base belongs_to :post end class Image < ActiveRecord::Base belongs_to :post end
N+1問題を解決するため、@postsの取得にincludesを使用して下記のように記述しました。
■コントローラでの取得 @posts = Post.includes([:video, :image]).limit(5)
ただ、@postsは必ずしもVideoやImageを持つ訳ではなく、取得した@postsがVideoのみの場合にBulletから下記のような警告が出ます。
Remove from your finder: :include => [:Image]
反対に、@postsがImageのみの場合には下記のような警告が出ます。
Remove from your finder: :include => [:Video]
このような場合、@postsの取得はコントローラーでどのように記述するのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/06 02:19