Created
April 21, 2014 02:56
-
-
Save rubyrider/11131049 to your computer and use it in GitHub Desktop.
Adding permalink field to an existing database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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