ruby-on-railsrubyruby-on-rails-5friendly-idancestry

Allow duplicate slugs with FriendlyId & Ancestry Gem in Rails 5


I am using FriendlyId & Ancestry Gem in my Rails 5 app. I am creating silo marketing pages for my primary services and need to create duplicate slugs that are on separate ancestry parent paths. I included my initial seed file below for reference.

As you can see, my root paths are:

www.mysite.com
www.mysite.com/events
www.mysite.com/events/san-diego
www.mysite.com/events/orange-county
www.mysite.com/events/los-angeles
www.mysite.com/events/riverside

** Here are the duplicate slugs I need **

www.mysite.com/events/san-diego/birthday-parties
www.mysite.com/events/orange-county/birthday-parties
www.mysite.com/events/los-angeles/birthday-parties
www.mysite.com/events/riverside/birthday-parties
...

As you can see although the slugs are duplicates, they sit off different parent paths. How can I validate the slug based on the parent so it will allow a duplicate slug to be created?

** db/seeds.rb **

home = Page.create(title: 'Home')

# home/events
events = Page.create(title: 'Events', parent: home)

# home/events/cityPage
san_diego = Page.create(title: 'San Diego', parent: events)
orange_county = Page.create(title: 'Orange County', parent: events)
los_angeles = Page.create(title: 'Los Angeles', parent: events)
riverside = Page.create(title: 'Riverside', parent: events)

# home/events/san-diego/eventPage
Page.create(title: 'Birthday Parties', parent: san_diego)
Page.create(title: 'School Events', parent: san_diego)
Page.create(title: 'Church Events', parent: san_diego)
Page.create(title: 'Corporate Events', parent: san_diego)
Page.create(title: 'Social Events', parent: san_diego)
Page.create(title: 'Community Events', parent: san_diego)

# home/events/orange-county/eventPage
Page.create(title: 'Birthday Parties', parent: orange_county)
Page.create(title: 'School Events', parent: orange_county)
Page.create(title: 'Church Events', parent: orange_county)
Page.create(title: 'Corporate Events', parent: orange_county)
Page.create(title: 'Social Events', parent: orange_county)
Page.create(title: 'Community Events', parent: orange_county)

# home/events/los-angeles/eventPage
Page.create(title: 'Birthday Parties', parent: los_angeles)
Page.create(title: 'School Events', parent: los_angeles)
Page.create(title: 'Church Events', parent: los_angeles)
Page.create(title: 'Corporate Events', parent: los_angeles)
Page.create(title: 'Social Events', parent: los_angeles)
Page.create(title: 'Community Events', parent: los_angeles)

# home/events/riverside/eventPage
Page.create(title: 'Birthday Parties', parent: riverside)
Page.create(title: 'School Events', parent: riverside)
Page.create(title: 'Church Events', parent: riverside)
Page.create(title: 'Corporate Events', parent: riverside)
Page.create(title: 'Social Events', parent: riverside)
Page.create(title: 'Community Events', parent: riverside)

** Screenshot of Database showing the hash that was added to the duplicate slugs **

enter image description here


Solution

  • You can use validates_uniqueness_of with scope.

    Eg.

    validates_uniqueness_of :slug, scope: :parent_id
    

    doing this way, it will validate uniqueness of this record, but allowing same slug if parent_id is different