aidiah aidiah - 6 months ago 11
Ruby Question

How to get actual post.id while using friendly_id

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

Answer

Try scoping your Post.find by the board:

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