osusume, novel. commentの3つのモデルがあり、
osusumeとnovelが1対多、
novelとcommentが1対多という関係になっています。
現在はosusumes#showで
1つのosusumeに紐づく全てのnovelを取得しています。
ruby
1 def show 2 @osusume = Osusume.where(:id => params[:id]).first 3 4 # postgreSQLでlikeがnilのレコードを後にしたいので以下のように実装しています 5 @novels_like_not_null = @osusume.novels.where.not(like: nil).order(like: :DESC) 6 @novels_like_null = @osusume.novels.where(like: nil) 7 @novels = @novels_like_not_null+@novels_like_null 8 9 @novel = Novel.new 10 end
ここで個別のnovelに紐づくcommentを取得、表示する方法がわかりませんでした。
どうかご教示お願いいたします。
以下、モデルの情報となります。
ruby
1class Osusume < ApplicationRecord 2 has_many :novels 3end
ruby
1class Novel < ApplicationRecord 2 belongs_to :osusume 3 has_many :comments 4end
ruby
1class Comment < ApplicationRecord 2 belongs_to :novel 3end
他、必要な情報がありましたらおっしゃってください。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。