1

鉴于我有一个嵌套在 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 集合。无论如何,我不想污染我的模型以使控制器身份验证工作。

(没关系,但我使用的是 mongoid)

4

1 回答 1

3

未经测试,但根据文档/代码,您应该能够将名称指定为第一个参数load_resource

load_resource :worker, :class => 'Worker', :through => :farm, :parent => false
于 2011-01-19T14:05:31.217 回答