Class Product < ActiveRecord::Base
has_friendly_id :name, :use_slug => true
end
在“产品”表中存储 slug 的最有效方法是什么。 我有复杂的查找查询,与“slugs”表的连接给我带来了性能瓶颈。
最佳答案
好吧,我知道这是一个老问题,但只是想提醒一下,对于可能偶然发现这个问题的其他人:
问题中的代码片段来自 FriendlyId 3.x,在这种情况下,您可以在表格中添加一列(除了 slug
之外的任何名称...我更喜欢使用 cached_slug
) 作为字符串并更新模型以显示
Class Product < ActiveRecord::Base
has_friendly_id :name, :use_slug => true, :cache_column => 'cached_slug'
end
从 friendly_id 4.x 开始,您只需将 slug
列作为 string
添加到表中,并使用新语法:
例如:
Class Product < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :slugged
end
有很多选项和方法可以充分利用 friendly_id,包括历史记录(避免 404)等......
更多信息:http://rubydoc.info/github/norman/friendly_id/master/frames
关于ruby-on-rails - 缓存 friendly_id 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1347631/