Skip to content

Instantly share code, notes, and snippets.

Created November 2, 2009 00:14
Show Gist options
  • Select an option

  • Save anonymous/223815 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/223815 to your computer and use it in GitHub Desktop.
class Tag < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
validates_uniqueness_of :tag, :scope => [:taggable_type, :taggable_id]
before_save :create_objects_for_each_tags
after_save :return_objects
def return_objects
if @objects
return @objects
else
return self
end
end
attr_writer :tags
def create_objects_for_each_tags
if @tags.is_a? Array
self.tag = @tags.pop
@objects = @tags.map do |tag_name|
clone = self.clone
return clone.update_attributes!(:tag => tag_name)
end
elsif @tags.is_a? String
self.tag = @tags
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment