RoR ActiveResource嵌套资源:3.2.1和3.0.7之间的差异

时间:2012-02-26 23:33:53

标签: ruby-on-rails jruby activeresource

我有两个简单的模型,clown.rb和funny_hat.rb。

ARes 3.0.7

clown = Clown.new({:funnyHats => [{ :id => "123" },{ :id => "456" }]})
p clown
#<Clown:0x3091b721 @prefix_options={},
@attributes={"funnyHats"=>[#<FunnyHat:0x7fd8623b @prefix_options={}, @attributes={"id"=>"123"}>, #<FunnyHat:0x3f18dc75 @prefix_options={}, @attributes={"id"=>"456"}>]}>

ARes 3.2.1

clown = Clown.new({:funnyHats => [{ :id => "123" },{ :id => "456" }]})
p clown
#<Clown:0x1f38b39c @prefix_options={}, @persisted=false, 
@attributes={"funnyHats"=>[#<Clown::FunnyHat:0xbd8632b @prefix_options={}, @persisted=false, @attributes={"id"=>"123"}>, #<Clown::FunnyHat:0x44adf91a @prefix_options={}, @persisted=false, @attributes={"id"=>"456"}>]}>

来自ARes 3.0.7的ARes 3.2.1的嵌套资源似乎有所不同。注意如何在ARes 3.2.1输出中,“Clown :: FunnyHat”节点。我记得当没有声明FunnyHat.rb模型时,这与ARes 3.0.7一起发生。

问题:这是一个可以解决的问题吗?

更新

如果我在ARes 3.2.1中执行以下操作,我会得到不同的结果。

funny_hat = FunnyHat.new()
clown = Clown.new({:funnyHats => [{ :id => "123" },{ :id => "456" }]})
p clown
#<Clown:0x506d043e @prefix_options={}, @persisted=false, 
@attributes={"funnyHats"=>[#<FunnyHat:0x70596c33 @prefix_options={}, @persisted=false, @attributes={"id"=>"123"}>, #<FunnyHat:0x381ba0c6 @prefix_options={}, @persisted=false, @attributes={"id"=>"456"}>]}>

这个问题的原因是因为如果节点是“Clown :: FunnyHat”,它不会映射到“FunnyHat”模型,因此实例没有“FunnyHat”的任何实例方法或属性“对象。

另一个临时黑客

将以下内容放在某个初始化步骤或before_filter

Dir.glob("#{Rails.root}/app/models/*.rb").sort.each { |file| require_dependency file }

感谢https://github.com/rails/rails/issues/5148

我开始认为ActiveResource 3.2.1是ActiveResource 3.0.7的主要回归。

0 个答案:

没有答案
相关问题