I have a pretty simple HABTM set of models
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts
end
class Post < ActiveRecord::Base
Extract the tag name for security. Check whether or not the tag exists in your tags table, then create it if it doesn't:
name = params[:tag][:name]
@new_tag = Tag.where(name: name).first_or_create
Then check whether it exists within this specific collection, and push it if it doesn't:
@taggable.tags << @new_tag unless @taggable.tags.exists?(@new_tag)