0

I currently have my post model using friendly_id to replace Post.id with Post.pid: friendly_id :pid, :use => :scoped, :scope => :id

How can I get the actual id of a post instead of the slug so that when a reply saves it saves Post.id and not Post.slug to Reply.post_id.

I need to do this because replies are appearing to posts created on other boards due to post.pid not being unique

class RepliesController < ApplicationController
  def create
    @board = Board.friendly.find(params[:board_id])
    @post = Post.friendly.find(params[:post_id])
    @reply = @post.replies.create(reply_params)
    @post.touch
    redirect_to @board
  end
  private
    def reply_params
      params.require(:reply).permit(:name, :email, :subject, :comment, :reply_file)
    end
end
indus
  • 103
  • 1
  • 10
  • Unrelated to your question: you can add `touch: true` to your `Post.belongs_to :board` association and save a line of code in your controller. – BM5k May 11 '16 at 05:49

1 Answers1

1

Try scoping your Post.find by the board:

@post = @board.posts.friendly.find params[:post_id]

BM5k
  • 1,210
  • 10
  • 28