Reputation: 13
Here I have some code:
def show
@post = Post.find()
end
end
the user model has has_many :posts and the post model has belongs_to :user. Now how can I make the show action only display posts by the user logged in. User logged in is "current_user"?
Upvotes: 0
Views: 111
Reputation: 54882
You could do this:
def show
@post = Post.where(user_id: current_user.id)
end
But there is glitch with the variable, you should rename @post
to @posts
since this will be an array of several Posts (if so, don't forget to change the variable in your view too!).
Upvotes: 1