【问题标题】:Rails console: attribute returns false, but is shown correctly with to_yamlRails 控制台:属性返回 false,但使用 to_yaml 正确显示
【发布时间】:2013-04-20 17:21:29
【问题描述】:

也许我遗漏了一些东西,但我做了研究,无法找出为什么会这样。我正在使用 Rails 2.3.16 和 ruby​​ 1.9.3,由一个巨大的 ibm-db2 支持。

假设我有一个类 Foo(我尝试在这里构建一个简短的示例)

class Foo < ActiveRecord::Base
end

该类具有属性:电话(整数)和邮件(字符串)。

当我使用 Rails 控制台进行调试时

bundle exec ./script/console      , or
RAILS_ENV=production bundle exec ./script/console

我可以通过以下方式加载一个 Foo 实例:

f = Foo.find_by_phone(123)
=> #<Foo phone: false, mail: "test@example.org">

如果我尝试使用 getter 方法获取 phone-attribute:

f.phone
=> false

但是信息必须在那里,因为 rails i) 找到了对象,并且 ii) 你可以这样做 yaml-output:

y f
---- !ruby/object:Foo
attributes: 
 phone: 123
 mail: test@example.org
attributes_cache: {}

谢谢大家提前帮助我,我确定我忽略了一些事情!

【问题讨论】:

  • Foo.phone_before_type_cast 是什么样的,Table 的架构是什么样的?
  • 你的班级有电话的 attr_accessor 吗?
  • @Doon: NoMethodError: undefined method `phone_before_type_cast' for ... @megas: 两者都没有。我尝试添加 attr_accessor :phone,然后 f.phone 返回 nil,而 yaml 版本保持不变。
  • @muistooshort: create_table "foo", :id => false, :force => true do |t| t.integer :phone t.string :mail end

标签: ruby-on-rails db2 rails-activerecord ruby-on-rails-2


【解决方案1】:

问题似乎是,ActiveRecord 将 DB2 短类型解释为布尔值。是否有可能改变这种(愚蠢的?)行为?

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 1970-01-01
    • 2014-08-17
    • 2019-09-22
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多