railsの2重になった繰り返し処理で@posts.books.user.avatarを取得しています。しかし、重複したavatarは取り除いて表示させたいです。どなたか、お知恵をお貸しいただけたらと思います。よろしくお願いいたします。
posts/index.html.erb
ruby
1<% @posts.each do |post| %> 2 <%= link_to post.title, micropost %> 3 <% post.books.each do |book| %> 4 <%= image_tag(book.user.avatar.thumb.url %> 5 <% end %> 6<% end %>
posts_controller.rb
ruby
1class PostsController < InheritedResources::Base 2 3 def index 4 @posts = Post.page(params[:page]).per(5) 5 end 6 7end
post.rb
class Post < ApplicationRecord belongs_to :user has_many :books, dependent: :destroy end
book.rb
class Book < ApplicationRecord belongs_to :post end
あなたの回答
tips
プレビュー