tl; dr:在 rails 控制台中运行时include MyModule
将MyModule
's 的功能带入范围,但在 rails 模型中则不会。我不明白并且希望能够在我的模型中访问这些功能。
我有这个文件lib/zip_validator.rb
,我想用它来验证我的User
模型的用户输入。
module ActiveModel::Validations::HelperMethods
def validates_zip(*attr_names)
validates_with ZipValidator, _merge_attributes( attr_names )
end
end
class ZipValidator < ActiveModel::EachValidator
def validate_each( record, attr_name, value )
unless is_legitimate_zipcode( self.zip )
record.errors.add( attr_name, :zip, options.merge( value: value ))
end
end
end
在 Rails 控制台中,我可以做
irb(main):005:0> include ActiveModel::Validations::HelperMethods
=> Object
irb(main):006:0> validates_zip
NoMethodError: undefined method `validates_with' for main:Object
from /home/bistenes/Programming/myapp/lib/zip_validator.rb:3:in `validates_zip'
from (irb):6
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
看!显然它找到了validates_zip
,因为它抱怨该方法中的调用。当我真正尝试从将要使用它的模型中调用它时,也许这种抱怨会消失,app/models/user.rb
:
class User < ActiveRecord::Base
include ActiveModel::Validations::HelperMethods
attr_accessible :first_name, :last_name, :zip, :email, :password,
:password_confirmation, :remember_me, :confirmed_at
validates_presence_of :first_name, :last_name, :zip
validates_zip :zip
当我尝试启动服务器(瘦)时,它会出错:
/usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined method `validates_zip' for #<Class:0x00000003e03f28> (NoMethodError)
from /home/bistenes/Programming/myapp/app/models/user.rb:38:in `<class:User>'
世界上到底发生了什么?为什么服务器找不到控制台找到的功能?