【问题标题】:ActiveRecord doesnt fetch certain fieldsActiveRecord 不获取某些字段
【发布时间】:2017-05-09 08:43:33
【问题描述】:

我正在使用一个设计模型进行注册,其中包含了其他字段。

这是我的用户表架构

 create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string   "provider",                             default: "email", null: false
    t.string   "uid",                                  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.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.string   "username",                                               null: false
    t.string   "email",                                                  null: false
    t.integer  "role",                                 default: 0,       null: false
    t.datetime "premium_expires_at"
    t.string   "token",                                default: "",      null: false
    t.text     "tokens",                 limit: 65535
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "avatar"
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
    t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree
    t.index ["username"], name: "index_users_on_username", unique: true, using: :btree
  end

但是,当通过 activeRecord (User model) 查询表时,我无法获取属于设计的某些列。

例子

userList = User.select("id, confirmation_token, confirmed_at, username, uid, role")

记录 userList 给我

#<ActiveRecord::Relation [#<User id: 1, uid: "foo@foo.com", username: "bowie", role: "admin", avatar: nil>, #<User id: 2, uid: "bar@bar.com", username: "wilson", role: "user", avatar: nil>, #<User id: 3, uid: "sp@sp.com", username: "Cartman", role: "user", avatar: nil>]> 

confirmation_token 字段和confirmed_at 令牌字段未被检索。还有什么我应该做的吗?

【问题讨论】:

  • puts userList.confirmation_token 或 puts userList.confirmed_at 打印什么?
  • hmm .. 当我以 user.confirmation_token 的身份循环遍历数组时,我实际上能够检索值。但它似乎仍然不是数组的一部分。更像是一种检索值的方法

标签: ruby-on-rails ruby activerecord devise


【解决方案1】:

请查看设计中的Authenticatable 模块。他们专门将这些属性列入黑名单(参见BLACKLIST_FOR_SERIALIZATION 常量)进行序列化。所以,我认为这就是您无法在控制台中看到它们的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2022-10-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多