【问题标题】:how to fetch value of a column which has some default value rails mysql如何获取具有一些默认值的列的值 rails mysql
【发布时间】:2014-11-27 09:00:50
【问题描述】:

我有一个 User 模型,具有属性 email、name ... 我想添加一个默认值为 true 的列 has_agreed。所以我添加了迁移并运行它,它在数据库中正确显示。现在,当我获取值 @user.has_agreed 时,它不会返回任何值。我在可访问的属性(Rails 3.2)中写了 has_agreed。 如果我通过此特定用户的更新命令手动更改数据库的内容,以将 id 为 1 的用户的 has_agreed 值更改为 false,然后运行相同的调用 @user.has_agreed,它会返回 false。

我有历史数据,我想添加这个默认列,不能也不打算通过数据库手动更新这个列。如何获得价值?

此外,问题仅在于用户表中的旧用户。对于新用户来说,它工作正常。

【问题讨论】:

    标签: ruby-on-rails-3 ruby-on-rails-3.2 mysql2


    【解决方案1】:

    在 user.rb 中

    class User def has_agreed # use this line code. if has_agreed not equal false # read_attribute(:has_agreed) || default_velue if read_attribute(:has_agreed).blank? ## default value true else read_attribute(:has_agreed) end end end

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 2012-11-27
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      相关资源
      最近更新 更多