ruby-on-rails Rails Active Storage找不到有效的模型关联

vfwfrxfs  于 2023-04-22  发布在  Ruby
关注(0)|答案(1)|浏览(145)

我希望你做得很好.我试图做的是转移回形针功能,以积极的存储,但我面对这个问题在过去两天,但无法找到一个解决方案.也许你可以帮助我与它.
我有两个模型library_document.rb和document.rb
这些模型彼此之间具有以下关联:

class LibraryDocument < ApplicationRecord

belongs_to :document

end
class Document < ApplicationRecord
  belongs_to :company
  has_many :library_documents, foreign_key: "document_id", dependent: :nullify
  
  do_not_validate_attachment_file_type :attached_file
  has_one_attached :attached_file
  validates_attachment :attached_file,
                        size: { less_than: 5.megabytes, message: 'File is too large' }
end

当我运行时,尝试在library_documents_controller中的index方法中访问文档,方法如下:

class LibraryDocumentsController < AuthenticatedController

  def index
    documents = @current_company.documents
    render json: { documents: documents}
  end
end

它给了我以下错误:

NameError - Rails couldn't find Document association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass.:
  app/controllers/library_documents_controller.rb:6:in `index'
  app/controllers/application_controller.rb:41:in `block in set_user_time_zone'
  app/controllers/application_controller.rb:41:in `set_user_time_zone

此外,在Rails控制台中执行Document.last时,它给了我以下错误:

NoMethodError: undefined method `before_attached_file_post_process' for Document:Class
Did you mean?  before_avatar_post_process
from /home/ubuntu/.rvm/gems/ruby-3.0.4/gems/activerecord-7.0.2.4/lib/active_record/dynamic_matchers.rb:22:in `method_missing'

对不起,这个问题似乎很长,但我尽力解释我的问题。

wtlkbnrh

wtlkbnrh1#

我认为你应该修改Msp::Templates::Document模型中的代码。
从这个

validates_attachment :attached_file,
                     size: { less_than: 5.megabytes, message: 'File is too large' }

validates :attached_file,
           size: { less_than: 5.megabytes, message: 'File is too large' }

看起来before_attached_file_post_process方法属于paperclip gem,您不再使用它了。

相关问题