加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ActiveRecord枚举返回nil,即使它有值

发布时间:2020-12-17 03:49:25 所属栏目:百科 来源:网络整理
导读:我有一个枚举,它有一个存储值,但getter返回nil. 我有以下代码: class Property ActiveRecord::Base enum building_type: [ :none_building_type,:pre_war_low_rise,:pre_war_mid_rise,:pre_war_high_rise,:post_war_low_rise,:post_war_mid_rise,:post_war_
我有一个枚举,它有一个存储值,但getter返回nil.

我有以下代码:

class Property < ActiveRecord::Base
  enum building_type:   [
    :none_building_type,:pre_war_low_rise,:pre_war_mid_rise,:pre_war_high_rise,:post_war_low_rise,:post_war_mid_rise,:post_war_high_rise,:pre_war_loft,:post_war_loft,:pre_war_hotel,:post_war_hotel
  ]

如果我在控制台中执行以下操作:

Property.last

我明白了

Property Load (0.8ms)  SELECT  "properties".* FROM "properties"   ORDER BY "properties"."id" DESC LIMIT 1
 => #<Property id: 3,created_at: "2014-11-17 15:39:34",updated_at: "2014-11-17 16:28:48",name: "PropertyName",property_type: 1,description: "adsdsaads",building_type: "4">

但如果我这样做

Property.last.building_type

我明白了

Property Load (0.5ms)  SELECT  "properties".* FROM "properties"   ORDER BY "properties"."id" DESC LIMIT 1
 => nil

解决方法

看起来你的building_type数据库条目的字段类型是一个字符串,而不是整数(这是枚举根据 documents映射到的).尝试更改数据库架构,它应该工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读