mangotango2005
mangotango2005

Reputation: 13

Displaying posts by specific user?

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

Answers (2)

MrYoshiji
MrYoshiji

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

dimuch
dimuch

Reputation: 12818

def show
    @posts = current_user.posts
end

Upvotes: 0

Related Questions