songの中にcommentがあり、1対多の関係になっていて、commentはそれぞれ画像を持っているのですが、
song一覧にcommentにある一番いいねの数が多いimageを載せたいです。
<% @songs.each do |song| %> <div class="col-md-4 col-sm-6 portfolio-item"> <a class="portfolio-link" data-toggle="modal" href="#portfolioModal1"> <div class="portfolio-hover"> <div class="portfolio-hover-content"> <i class="fa fa-plus fa-3x"></i> </div> </div> <%= image_tag song.comments.image.url(:medium).order(:cached_votes_up => :desc).limit(1) %> <img class="img-fluid" src="/assets/noimage.jpg" alt=""> </a> <div class="portfolio-caption">
としたのですが、エラーが出ます。
commentsにはimageがあるので、なぜここでimageがno method errorになるのかわかりません。
また、アソシエーションはできているはずです
class Song < ActiveRecord::Base acts_as_votable belongs_to :user has_many :comments, :dependent => :destroy end
class Comment < ActiveRecord::Base acts_as_votable belongs_to :user belongs_to :song has_attached_file :image, styles: { :medium => "400x400#", :thumb => "180x180#" }, :default_url => "noimage.jpg" validates_attachment_content_type :image, content_type: /\Aimage/.*\Z/ end
どうしたら解消できるでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。