I have Post and category models
Post
belongs_to :category
Category
has_many :posts
On a category show page, I’m able to display a list of all posts which belongs to this category by
<% Post.where(category_id: @category.id).each do |post| %>
....
<% end %>
categories_controller ....
def show
@category = Category.friendly.find(params[:id])
@categories = Category.all
end
How can I display a list of related posts which belongs to the same category (or sharing the same category id), on one of the post’s show page. Thanks!
I assume you may do something like this in
posts_controller:BTW, a good practice is using scopes instead of
where:So that in the controller you may do