ID unique to nested model, not unique globally?

2020-05-08 01:15发布

I have nested routes for a has_many and belongs_to relationship.

resources :websites, :path => 'w' do
    resources :pages, :path => ''
end

I'm using friendly ID gem to replace the page ID in the URL with a slug.

So my URLs look like this

http://example.com/w/1-website-name/page-name

However, when a duplicate page name is added, friendly ID appends a random hash to it to make it unique, even if the page belongs to a different website.

Is there a way I can make it so page names only must be unique within the website they belong to?

I want to be able to do this:

http://example.com/w/1-website-one/home

and

http://example.com/w/2-website-two/home

1条回答
狗以群分
2楼-- · 2020-05-08 01:30

Adding scope might be an answer:

friendly_id :name, :use => :scoped, :scope => :website
查看更多
登录 后发表回答