Ruby验证在控制台中传递,在rake任务中失败

时间:2014-02-24 20:10:35

标签: ruby-on-rails ruby validation

这是我的EmailContact型号:

class EmailContact < ActiveRecord::Base
  validates :email, :presence => true, :email => true
end

我正在使用ruby gem valid_email

我在我的rails控制台中运行以下内容,与我稍后将展示的rake任务在同一环境中运行:

>>   email_contact = EmailContact.new(:email => 'a253545@gmail.com')

>>   email_contact.valid?

     true

因此,正如您所看到的,在rails控制台中,我正在构建EmailContact并且它是有效的。

然后我在我的rake任务中运行它:

list_entity = {:branch=>"Nashua Branch-YMCA of Greater Nashua", :branch_id=>"485", :call_type=>nil, :client_id=>"2264", :client_name=>"YMCA of Greater Nashua", :date_of_birth=>nil, :email=>"a253545@gmail.com", :first_name=>"Sridhar", :last_name=>"Tipirneni", :list_entity_id=>"277795", :mem_id=>"4085008", :mem_unit_id=>"2138728", :member_id=>"0213262-01", :membership_type=>"Dual 2 Adult Family", :membership_type_id=>"5203", :most_recent_join_date=>nil, :old_membership_type=>nil, :phone_number=>"(970)456-1010", :primary_language=>"English", :termination_date=>nil, :termination_reason=>nil, :unit_id=>"0213262", :unit_type=>nil, :visits=>nil, :"@i:type"=>"c:NpsListEntityDto"}

     email_contact = EmailContact.new(list_entity.except(:"@i:type"))
     puts email_contact.valid?

返回false。唯一的验证是email。为什么此电子邮件在我的控制台中成功验证但在我的rake任务中失败?

仅供参考,当我从:email => true模型中删除EmailContact并仅验证:email的存在时,它们都可以正常工作。所以问题肯定在我的验证的:email => true内,但我不明白为什么它在一个地方传递而在另一个地方失败。

修改

在我的控制台中,当使用完整的list_entity

时,我的模型看起来像这样
#<EmailContact id: nil, branch: "Nashua Branch-YMCA of Greater Nashua", branch_id: 485, call_type: nil, client_id: 2264, client_name: "YMCA of Greater Nashua", date_of_birth: nil, email: "a253545@gmail.com", first_name: "Sridhar", last_name: "Tipirneni", list_entity_id: 277795, mem_id: "4085008", mem_unit_id: "2138728", member_id: "0213262-01", membership_type: "Dual 2 Adult Family", membership_type_id: 5203, most_recent_join_date: nil, old_membership_type: nil, phone_number: "(970)456-1010", primary_language: "English", termination_date: nil, termination_reason: nil, unit_id: "0213262", visits: nil, loaded_at: nil, failed_at: nil, unit_type: nil, created_at: nil, updated_at: nil, list_id: nil>

在我的rake任务中,当我运行email_contact.inspect时,会返回:

#<EmailContact id: nil, branch: "Nashua Branch-YMCA of Greater Nashua", branch_id: 485, call_type: nil, client_id: 2264, client_name: "YMCA of Greater Nashua", date_of_birth: nil, email: "a253545@gmail.com", first_name: "Sridhar", last_name: "Tipirneni", list_entity_id: 277795, mem_id: "4085008", mem_unit_id: "2138728", member_id: "0213262-01", membership_type: "Dual 2 Adult Family", membership_type_id: 5203, most_recent_join_date: nil, old_membership_type: nil, phone_number: "(970)456-1010", primary_language: "English", termination_date: nil, termination_reason: nil, unit_id: "0213262", visits: nil, loaded_at: nil, failed_at: nil, unit_type: nil, created_at: nil, updated_at: nil, list_id: nil>

如您所见,它们完全相同 - 控制台模型有效,rake模型无效。

编辑2

我正在使用上面提到的valid_email gem。这是文件路径:

/Users/luigi/.rvm/gems/ruby-2.0.0-p247@hub/gems/valid_email-0.0.4/lib/valid_email/email_validator.rb

我所有的其他宝石都存储在这里。

值得一提的是,在验证失败之前我收到了此警告:

[deprecated] I18n.enforce_available_locales will default to true in
the future. If you really want to skip validation of your locale you
can set I18n.enforce_available_locales = false to avoid this message.

1 个答案:

答案 0 :(得分:0)

20小时后,我发现了这个问题。

使用savon,我的哈希中返回的所有字符串都被转换为Nori::StringWithAttributes的数据类型。编码是相同的(UTF-8),但课程不同。

在检查模型是否有效之前运行email_contact.email = email_contact.email.to_s可以解决问题。