slug memoization in the table

This commit is contained in:
Sam 2013-04-24 12:46:43 +10:00
parent c9ed7100b1
commit 81673dcde8

View file

@ -598,7 +598,24 @@ class Topic < ActiveRecord::Base
end end
def slug def slug
Slug.for(title).presence || "topic" unless slug = read_attribute(:slug)
return '' unless title.present?
slug = Slug.for(title).presence || "topic"
if new_record?
write_attribute(:slug, slug)
else
update_column(:slug, slug)
end
end
slug
end
def title=(t)
slug = ""
slug = (Slug.for(t).presence || "topic") if t.present?
write_attribute(:slug, slug)
write_attribute(:title,t)
end end
def last_post_url def last_post_url