ID unique to nested model, not unique globally?

最后都变了- 提交于 2019-12-04 07:05:14

问题


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:


Adding scope might be an answer:

friendly_id :name, :use => :scoped, :scope => :website


来源:https://stackoverflow.com/questions/41923056/id-unique-to-nested-model-not-unique-globally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!