I would use friendly_id.
It would look like this:
class Article < ActiveRecord::Base
extend FriendlyId
belongs_to :user
friendly_id :name, use: :scoped, scope: :user, slug_column: :permalink
In case someone picks the same article name, friendly_id
will calculate a slug
that doesn't clash with an existing one.
So, for example:
- Article with id #1 named "Hello" would have permalink
/user_id/news/hello
- Article with id #2 named also "Hello" would have permalink
/user_id/news/hello-2
If you don't want to use that gem, you can just use Rails
validations.
It'd look like this:
class Article < ActiveRecord::Base
extend FriendlyId
belongs_to :user
validates_uniqueness_of :permalink, scope: :user_id
Good old validates_uniqueness_of.
I hope it helps.