2016-08-01 75 views
1

我无法在特定地址的测试特定邮政编码的钢轨方法。我有一个在模型中被继承和验证的问题。它修复了用户输入查询不匹配,他们位于美国默认邮政编码的另一个问题。不过,我最终需要运行迁移来解决已存在缺陷直到这一点任何邮政编码。这给我带来了我的问题。有没有办法通过控制台来测试特定的关注点或方法?如何测试通过轨道控制台

我关心的是下面。

module StateMatchesZipCodeConcern 
    extend ActiveSupport::Concern 

    def verify_zip_matches_state 
    return unless zip.present? && state.present? 

    state_search_result = query_zip_code 

    unless state_search_result.nil? 
     return if state_search_result.upcase == state.upcase 
     return if validate_against_multi_state_zip_codes 
    end 
    errors[:base] << "Please verify the address you've submitted. The postal code #{zip.upcase} is not valid for the state of #{state.upcase}" 
    false 
    end 

    private 

    def query_zip_code 
    tries ||= 3 
    Geocoder.search(zip).map(&:state_code).keep_if { |x| Address::STATES.values.include?(x) }.first 
    rescue Geocoder::OverQueryLimitError, Timeout::Error 
    retry unless (tries -= 1).zero? 
    end 

    def validate_against_multi_state_zip_codes 
    ::Address::MULTI_STATE_ZIP_CODES[zip].try(:include?, state) 
    end 
end 

我试过User.last.address.validate_against_multi_state_zip_codes?和风与方法失踪。有人会知道我在这里错过了什么吗?

回答

3

我假设你,包括你的User模型StateMatchesZipCodeConcern

这意味着该方法validate_against_multi_state_zip_codes将在User实例的方法。

您试图在返回Useraddress方法时调用此方法,这是抛出NoMethodError的方法。另外,你已经制定了这个方法private,所以你将无法再调用它。

把上述方法的private线模块中并调用它像这样:User.last.validate_against_multi_state_zip_codes?

+0

你是真棒!非常感谢! – kdweber89

+1

http://i.imgur.com/9ld8MlW.gif – DiegoSalazar