1

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!

Ganesh
  • 1,924
  • 1
  • 18
  • 31
Charlie
  • 236
  • 2
  • 13
  • Category.friendly.includes(:posts) would fetch you all posts related to Friendly category – praga2050 Apr 07 '18 at 05:39
  • @praga2050 any update on the syntax to use on the show page? Your approach works but just looping the same post not showing the related. Thanks – Charlie Apr 07 '18 at 08:58

3 Answers3

1

I assume you may do something like this in posts_controller:

def show
  @post = Post.find(params[:id])

  @relative_posts = Post.where(category_id: @post.category_id)
end

BTW, a good practice is using scopes instead of where:

Post
belongs_to :category
scope :of_category, ->(category) { where(category_id: category) }

So that in the controller you may do

@relative_posts = Post.of_category(@post.category)
vbyno
  • 546
  • 1
  • 5
  • 5
  • Thanks for the feedback. Can you please share the syntax to use on the post show page to fetch the related posts? I tried <% @related_posts.each do |post| %>....<% end %> and also <% Category.friendly.includes(:posts).each do |post| %>...<% end %> but both display list of the same post on its own page, not fetching related posts – Charlie Apr 07 '18 at 06:31
  • Charlie, have you looked into the database? Are there any posts with needed `category_id` besides the current one? – vbyno Apr 07 '18 at 17:21
  • Yes, I have checked in rails console and all the post have category_id. Also list of posts with the same category_id show fine on the specific category page – Charlie Apr 08 '18 at 02:20
1

No need to use where condition as you already have associations between Post and Category. So you can modify following code

Post.where(category_id: @category.id)

as

@category.posts

So your show action present in posts_controller.rb should look like this

def show
  @post = Post.find(params[:id])
  @relative_posts = @post.category.posts
end

Now your view should be similar to

<% @related_posts.each do |post| %>
   // disply details of individual post 
<% end %> 

Or you can avoid @related_posts variable by modifying each loop as <% @post.category.posts.each do |post| %>

Hope this will help you.

Ganesh
  • 1,924
  • 1
  • 18
  • 31
  • Thanks @Ganesh, that makes sense in the posts_controller. what is the full syntax to use on the post show page to fetch the related posts? – Charlie Apr 07 '18 at 07:34
  • using the above still displays **the same post** in multiples of related posts but not showing actual related posts. BTW I have used "related_posts" throughout instead of mixing with "relative_posts" – Charlie Apr 07 '18 at 08:26
  • I'm trying to put this logic together in the posts_controller `@posts = Post.where.not(id: @post.id).belongs_to_category(@post.category.post, any: true)` I know .belongs_to_category is not a correct syntax but any idea how i can put it? – Charlie Apr 08 '18 at 03:05
  • This query `@posts = @category.posts.where.not(id: @post.id)` might be help you. – Ganesh Apr 08 '18 at 07:10
  • Thanks all for your help. It still displays the same post in multiples of the related posts. May be there is something wrong with my db. I will play around it to find out. – Charlie Apr 08 '18 at 20:19
  • Please check with different category posts, you might be checking post with same categories. – Ganesh Apr 09 '18 at 05:05
  • I checked but still same issue. Checked the app in detail, I just can't understand why it displays the same post repeatedly – Charlie Apr 16 '18 at 00:14
0

My bad, all along I was calling '@post' in the show view instead of 'post'

Controller should be:

@related_posts = Post.where(category_id: @post.category_id)

and

Post show view:

<% @related_posts.each do |post| %>
<%= post.name %>
<% end %>

Thanks all for your contributions

Charlie
  • 236
  • 2
  • 13