【发布时间】:2009-08-28 15:10:41
【问题描述】:
Class Product < ActiveRecord::Base
has_friendly_id :name, :use_slug => true
end
在“产品”表中存储 slug 的最有效方法是什么。 我有复杂的查找查询,与“slugs”表的连接给我带来了性能瓶颈。
【问题讨论】:
标签: ruby-on-rails seo friendly-id
Class Product < ActiveRecord::Base
has_friendly_id :name, :use_slug => true
end
在“产品”表中存储 slug 的最有效方法是什么。 我有复杂的查找查询,与“slugs”表的连接给我带来了性能瓶颈。
【问题讨论】:
标签: ruby-on-rails seo friendly-id
好的,我知道这是一个老问题,但只是想指出,对于可能偶然发现此问题的其他人:
问题中的代码 sn-p 来自 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
【讨论】:
Friendly_id 现在有内置的 slug 缓存。
【讨论】: