Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails:如何限制Mongoid中散列字段的键?_Ruby On Rails_Mongoid - Fatal编程技术网

Ruby on rails RubyonRails:如何限制Mongoid中散列字段的键?

Ruby on rails RubyonRails:如何限制Mongoid中散列字段的键?,ruby-on-rails,mongoid,Ruby On Rails,Mongoid,例如,我想只为字段url class Person include Mongoid::Document field :first_name field :url, type: Hash end 我想要这份工作 def set_vals self.first_name = 'Daniel' self.url = {'home_page' => 'http://www.homepage.com'} save end 而这次失败了 def set_vals sel

例如,我想只为字段
url

class Person
  include Mongoid::Document
  field :first_name
  field :url, type: Hash
end
我想要这份工作

def set_vals
  self.first_name = 'Daniel'
  self.url = {'home_page' => 'http://www.homepage.com'}
  save
end
而这次失败了

def set_vals
  self.first_name = 'Daniel'
  self.url = {'unknown' => 'http://www.homepage.com'}
  save
end

添加模型验证,检查密钥,如果存在不需要的密钥,则向模型中添加错误。

具体如何操作?