I'm using the friendly_id to add custom slugs to my models and their corresponding urls. Currently I have a setup where a Post
belongs to a Board
. There will undoubtedly be cases where a post will have an identical title to another but from a different board. Often times I've noticed sites (SO included) prepend a unique set of numbers before the slug to insure there are no issues with uniqueness:
https://stackguides.com/questions/123456/my-example-question
I was wondering what would be the best approach to accomplish this? This couldn't be done solely through the routes file because there still poses the possibility of two or more identical posts being created. Would it be a combination of altering my model, routes file, and configuration of the friendly_id gem?
My end goal is to have a url generated like this for my posts:
https://example.com/boards/example-board/123456/example-post
class Board < ApplicationRecord
extend FriendlyId
has_many :posts
friendly_id :name, use: :slugged
end
class Post < ApplicationRecord
extend FriendlyId
belongs_to :board
friendly_id :title, use: :slugged
end
resources :boards do
scope module: :boards do
resources :posts
end
end