鉴于我有一个嵌套在Farm模型下的Worker模型,如何在Workers控制器(称为FarmWorkersController)中正确加载Worker资源?我试过这个......
class FarmWorkersController < ApplicationController
load_resource :farm, :parent => true
load_resource :class => 'Worker', :through => :farm, :parent => false
# Note that :parent and :class need to be specified on the Worker resource line,
# as the name of the controller (FarmWorkersController) is different from Worker model name
end
...但我收到了错误
undefined method `farm_workers' for #<Farm:0xa87670c>
请注意,如果我在Farm模型中定义了farm_workers()getter,它返回了Workers集合,那么我不会收到错误 - 尽管未为index集合加载Workers集合。无论如何,我不想污染我的模型以使控制器认证工作。
(应该没关系,但我使用的是mongoid)
答案 0 :(得分:3)
未经测试,但根据文档/代码,您应该能够将名称指定为load_resource
的第一个参数:
load_resource :worker, :class => 'Worker', :through => :farm, :parent => false