Skip to content

Instantly share code, notes, and snippets.

@rujmah
Created May 21, 2012 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rujmah/2761594 to your computer and use it in GitHub Desktop.
Save rujmah/2761594 to your computer and use it in GitHub Desktop.
Mongoid_tags
class Tag
include Mongoid::Document
field :name
embedded_in :taggable, polymorphic: true
end
class Category
include Mongoid::Document
embeds_many :tags, class_name: "Tag", as: :taggable
def tag_list
tags.map(&:name).join(",")
end
def tag_list=(s)
tags.clear
s.split(",").map(&:strip).each { |n| tags.new(name: n) }
end
end
# c = Category.create
# c.update_attributes :tag_list => "a"
# c.reload
# c.tag_list # => "a"
# c.update_attributes :tag_list => "a,b"
# c.reload
# c.tag_list # => "a,a,b" :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment