Rails Rails 4.2.10 -> 5.0.0 的一个跟 enum 有关很隐蔽的差异

Catherine · February 07, 2020 · Last by khiav223577 replied at February 16, 2020 · 2651 hits

项目从 4.2.10->5.0.0。

在 4.2.10 中 (其他版本未测试)

class User < ActiveRecord::Base
  enum state: { good: 0, bad: 1}
end

在 4.2.10 中

User.first[:state]

会得到对应的整数的值。 但在 5.0.0 中,会变成对应的 string 的值

Rails5 改了,要用

User.first.state_before_type_cast

请问有变数的作法吗? 还是只能够这样子:

User.first.send(:"#{column_name}_before_type_cast")
Catherine closed this topic. 20 Apr 19:27
You need to Sign in before reply, if you don't have an account, please Sign up first.