【问题标题】:to_json not working because reflections are wrongto_json 不起作用,因为反射错误
【发布时间】:2010-09-27 07:29:12
【问题描述】:

我有一个名为 Foo 的类,它看起来像这样:

class Foo < ActiveRecord::Base 
  belongs_to othermodel 
end 

还有一个类 Bar,如下所示:

class Bar  < Foo 
   belongs_to thirdmodel
end 

当我尝试使用以下命令序列化 bar 时:bar.first.to_json(:include => :othermodel),它在生产模式下失败。奇怪的是它适用于 开发环境。我四处挖掘,发现 bar.first.class.reflections.keys 仅包含“thirdmodel”,不包含 生产模式中的“othermodel”。

我无法解释,在 rails 2 和开发环境中 工作完美。

错误是:

NoMethodError: undefined method `macro' for nil:NilClass
   from ~/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serialization.rb:41:in `block in serializable_add_includes'

【问题讨论】:

    标签: ruby-on-rails-3


    【解决方案1】:

    问题在于思考狮身人面像。删除它并用自己的解决方案替换它,现在它可以完美地工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2015-10-12
      • 2018-12-30
      相关资源
      最近更新 更多