Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments, dependent: :destroy
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
class PostsController < ApplicationController
def index
@posts = Post.includes(:user, :comments).order('created_at DESC')
end
end
<% @posts.each do |post| %>
<%= link_to post.user.user_name, user_path(post.user) %> #кто написал пост
<% post.comments.each do |comment| %> #проходимся по комментам
<%= comment.user.user_name%> # кто написал коммент
<% end %>
<% end %>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question