Skip to content

Instantly share code, notes, and snippets.

@kevinpang
Created April 22, 2011 15:58
Show Gist options
  • Save kevinpang/936933 to your computer and use it in GitHub Desktop.
Save kevinpang/936933 to your computer and use it in GitHub Desktop.
Initial tagging implementation
def Post
attr_accessor :tag_names
has_and_belongs_to_many :tags
after_save :update_tags
private
def update_tags
tags.clear
if tag_names.to_s == ''
return
end
tag_names.split(/,/).each do |tag_name|
tag_name.strip!
tag = Tag.find_or_create_by_name(tag_name)
if !tags.exists?(tag.id)
tags << tag
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment