我有一个MongoMapper模式,并试图将 com-de-limit-limit-limit-limit-str-str带转换成将储存的Array。
主要问题是,像<代码>tags = “第一、二、三”代码”这样的缩略语没有转换成数据库中的一种阵列,如<代码>[“第一”、“第二”、“第三”]。 而是作为<代码>[“第一、二、三”]代码>。
There are some other strange things going on as well:
1) In preen_tags I have to include the unless tags.nil? after every line
2) in preen_tags, using the debugger tags
returns nil
这里我的模式
class Template
include MongoMapper::Document
validate :validate_tags
after_validation :preen_tags
key :template_id, ObjectId
key :title, String
key :description, String
key :tags, Array
timestamps!
def validate_tags
errors.add_to_base "You Must Enter At Least 1 Tag." if tags.blank?
end
def preen_tags
#return if tags.nil? #why doesn t this work??
#only alphanumeric chars allowed, except hyphens and commas
tags = tags[0] if tags.is_a?(Array)
tags = tags.gsub(/[^0-9a-z-,]/i, ) unless tags.nil?
#convert spaces to hyphens
tags = tags.gsub(/s/, - ) unless tags.nil?
tags = tags.split(",") unless tags.nil?
end
end