Class Product < ActiveRecord::Base
has_friendly_id :name, :use_slug => true
end
将蛞蝓存储在“产品”表中的最有效方法是什么。我有复杂的查找查询,并且与“slugs”表的连接给我带来了性能瓶颈。
Class Product < ActiveRecord::Base
has_friendly_id :name, :use_slug => true
end
将蛞蝓存储在“产品”表中的最有效方法是什么。我有复杂的查找查询,并且与“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
列作为 a添加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 缓存。