我有一个使用许多自定义 ActiveModel 验证器的模型。安装 rails_admin 时出现以下错误
.rvm/gems/ruby-1.9.2-head/bundler/gems/rails_admin-0d80b74a60e7/lib/rails_admin/adapters/mongoid.rb:340:in `block in length_validation_lookup': undefined method `attributes' for #<MyCustomValidator:0x00000105c5aca0 @options={}> (NoMethodError)
from .rvm/gems/ruby-1.9.2-head/bundler/gems/rails_admin-0d80b74a60e7/lib/rails_admin/adapters/mongoid.rb:339:in `select'
from .rvm/gems/ruby-1.9.2-head/bundler/gems/rails_admin-0d80b74a60e7/lib/rails_admin/adapters/mongoid.rb:339:in `length_validation_lookup'
from .rvm/gems/ruby-1.9.2-head/bundler/gems/rails_admin-0d80b74a60e7/lib/rails_admin/adapters/mongoid.rb:98:in `block in properties'
如果我注释掉模型中的验证器调用,错误就会消失。想知道是否有解决问题的方法(除了破解 rails_admin 代码库)。
以下是骨架代码(仅相关部分)
class MyModel
include Mongoid::Document
....
validates_with MyValidator
end
class MyValidator < ActiveModel::Validator
def validate(record)
end
end
正如我在评论中提到的,添加一个返回空数组的“属性”方法可以解决问题。