ActiveRecord字段在类型转换后返回nil

时间:2014-10-26 15:39:54

标签: ruby-on-rails activerecord ruby-on-rails-4 devise

我试图在Rails 4中使用Devise gem设置应用程序。尝试访问电子邮件字段时出现了一个非常奇怪的错误。电子邮件字段在数据库中有一个值,但是当我尝试直接访问它时,它返回nil,当我使用email_before_type_cast时,它返回一个正确的值。这是控制台输出:

2.1.3 :035 > User.first
User Load (67.2ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 7, email: "marcindobry@hotmail.com", encrypted_password: "$2a$10$wyXS5nNQAbzDLeXTrITCROqI2FBvKuN0YvzvTTE0mqO...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-10-26 12:35:55", last_sign_in_at: "2014-10-26 12:35:55", current_sign_in_ip: #<IPAddr: IPv4:127.0.0.1/255.255.255.255>, last_sign_in_ip: #<IPAddr: IPv4:127.0.0.1/255.255.255.255>, created_at: "2014-10-26 12:35:55", updated_at: "2014-10-26 12:35:55">
2.1.3 :036 > User.first.email
User Load (0.8ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" ASC LIMIT 1
=> nil
2.1.3 :039 > User.first.email_before_type_cast
User Load (0.5ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" ASC LIMIT 1
=> "marcindobry@hotmail.com"

user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, 
  :trackable, :validatable

end

schemba.rb

ActiveRecord::Schema.define(version: 20141025222540) do


  enable_extension "plpgsql"

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

end

有什么想法吗? :(

0 个答案:

没有答案