Skip to content

Instantly share code, notes, and snippets.

@rubyrider
Created April 21, 2014 02:56
Show Gist options
  • Save rubyrider/11131049 to your computer and use it in GitHub Desktop.
Save rubyrider/11131049 to your computer and use it in GitHub Desktop.
Adding permalink field to an existing database
# in migration
class AddPermalinkToPost < ActiveRecord::Migration
def change
add_column :posts, :permalink, :string
end
Post.find_each(&:save)
end
# in model
validates :title, presence: true
before_validation :generate_slug
private
def generate_slug
# title is the field
self.permalink = "#{self.title.parameterize}-#{self.id}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment