LoginSignup
0
0

More than 3 years have passed since last update.

投稿データとユーザー情報の結びつけ方法

Posted at

まず用意したのはpost.rb(models/post)

class Post < ApplicationRecord

  def user
    return User.find_by(id: self.user_id)
  end

end

そしてposts_controller

def show
@post = Post.find_by(id: params[:id])
@user = @post.user
end

・ポイント

post(投稿)のところにuserインスタンスメソッドを用意してあげる。

そしてposts_controllerの処理したいアクションにさっき用意したuserのインスタンスメソッドを入れてあげる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0