我正在尝试构建一个注释模型,例如App.net有一个注释:http://developers.app.net/docs/meta/annotations/。为了方便地将注释添加到许多模型,我使用了一个问题。
到目前为止,我有Annotation的模型:
class Annotation
include Mongoid::Document
field :key, :type => String
field :value
embedded_in :annotatable, polymorphic: true
# Some validations
attr_accessible :key, :value
attr_accessor :key, :value
end
关注点:
module Annotatable
extend ActiveSupport::Concern
included do
embeds_many :annotations, as: :annotatable
end
# Somewhat helper method to add annotaion only after they are valid.
def add_annotation( annotation )
if !annotation.valid?
return false
else
self.annotations.push annotation
return true
end
end
end
最后有一个模型,其中包含了关注点:
class User
include Mongoid::Document
include Mongoid::Timestamps
include Annotatable
# Much other stuff...
end
不幸的是,注释不会被保存: - / 有任何想法吗?