This is a copy/paste from my question over at
feel free to answer there if you want some reputation.
I’m trying to implement a tagging system in a Rails app similar to the
StackOverflow uses (where users enter in tags in a freeform textbox).
aware that there are gems that can do this, but I wanted to try to
it myself for the learning experience. I got it to work, but since I’m a
Rails newbie I’m concerned I’m not doing it the “right way”.
Here’s my current implementation:
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
This makes it easy for me to set up the tags for a post since all I have
do is set the “tag_names” attribute on my post object. When I save the
object, the after_save event fires and executes my update_tags function
which then handles creating tags and linking the post to them.
Are there any problems with my implementation?