1

在设置简单的“显示所有事物”视图时,我遇到了开拓者的问题。

手术

class Thing < ApplicationRecord
  class ShowAll < Trailblazer::Operation
    include Model
    model Thing, :all   #why :all is not working here?

    def process
    end
  end
end

控制器

class PageController < ApplicationController
  def index
    run Word::ShowAll
  end
end

为什么:all不能从数据库中获取所有东西,而是:find通过它的 id 获取一个?

4

2 回答 2

1

在你这样做的时候调用Trailblazer::Model#model只是一个覆盖该TrailBlazer::Operaration#model!方法的快捷方式。所以你似乎想要做的是:

class Thing < ApplicationRecord
  class ShowAll < Trailblazer::Operation
    def model!(params)
      Thing.all # add any filtering or pagination here
    end
  end
end

并且在您的控制器调用中,present而不是run这样它设置模型但不调用process操作的方法。

class PageController < ApplicationController
  def index
    present Word::ShowAll
  end
end
于 2016-11-19T14:25:31.203 回答
1

问 TRB 问题的最佳地点实际上是 Github 频道。

我不确定您在哪里找到该示例,因为它不应该在 AFAIK 中工作, :find 是我相信的快捷方式,我从未真正使用过它。

您的所有逻辑都应该在 process 方法中定义。http://trailblazer.to/gems/operation/1.1/api.html#process

话虽如此,尝试在没有某种分页的情况下获取所有记录是一个非常糟糕的主意,除非您 100% 确定您的表不会超过几十条记录。除非你知道你没有很大的负载。所以定义这种捷径是危险的。

于 2016-11-19T00:35:53.490 回答