paper_trail模型和用户关系不起作用

paper_trail model and User relationships not working

提问人:robertpostill 提问时间:3/2/2023 更新时间:3/2/2023 访问量:46

问:

我正在翻新旧的 Rails 应用程序,该应用程序有一个管理视图,显示对 paper_trail 上记录的对象的更改。

rpsecs 失败,并出现以下错误:

Failure/Error: - @versions.each do |version|

     ActionView::Template::Error:
       Association named 'author' was not found on PaperTrail::Version; perhaps you misspelled it?
     Shared Example Group: "successful response" called from ./spec/requests/admin/versions_spec.rb:20

查看规格,我可以看到:

  describe "#index" do
    describe "for booking" do
      let(:booking) { create(:booking) }

      subject { get "/bookings/#{booking.id}/versions" }

      before { booking.update!(handover_notes: "foo") }

      it_behaves_like "successful response"
    end

因此,该调用呈现一个 HAML 视图:

              - @versions.each do |version|
                %tr
                  %td 
                    %p= version.item_type
                    %p= "##{version.item_id}"
                  %td= version.event
                  %td= version.created_at.strftime("%d/%m/%Y %H:%M:%S")
                  %td
                    - if version.author
                      %span= version.author

果不其然,控制器有一个加载关系的:before_actionauthor

def load_versions
    @versions = @subject.all_paper_trail_versions
                        .includes(:author)
                        .order(created_at: :desc)
                        .page(params[:page])
                        .per(20)
    @versions_size = @versions.size
  end

所以由一个猴子修补的模型对象定义:author

module PaperTrail
  # PaperTrail::Version is a monkey-patch of the same class built-in in gem "paper_trail"
  class Version < ActiveRecord::Base
    include PaperTrail::VersionConcern

    belongs_to :author, class_name: "User", foreign_key: :whodunnit, optional: true
  end
end

因此,该属性似乎已定义(并且已经定义了大约 5 年)。所以我不确定为什么 rails 认为没有名为 author 的协会?author

Ruby-on-Rails Paper-Trail-gem

评论


答: 暂无答案