Rails Active Storage 无法找到有效的模型关联。

huangapple go评论59阅读模式
英文:

Rails Active Storage unable to find a valid model association

问题

I hope you are doing good.
I am trying to do is shift paper clip functionality to active storage but I am facing this issue for the past two days but unable to find a solution for it. Maybe you can help me with it.

I've two models
library_document.rb and document.rb

The models have the following associations, with each other:

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

When I run try to access documents in the index method in library_documents_controller, in the following way:

class LibraryDocumentsController < AuthenticatedController
  def index
    documents = @current_company.documents
    render json: { documents: documents }
  end
end

it gives me the following error:

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

Moreover, on doing Document.last in the rails console it gives me the following error:

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'

Sorry the question seems very long, but I tried my best to explain my problem.

英文:

I hope you are doing good.
I am trying to do is shift paper clip functionality to active storage but I am facing this issue for the past two days but unable to find a solution for it. Maybe you can help me with it.

I've two models
library_document.rb and document.rb

The models have the following associations, with each other:

class LibraryDocument &lt; ApplicationRecord

belongs_to :document

end
class Document &lt; ApplicationRecord
  belongs_to :company
  has_many :library_documents, foreign_key: &quot;document_id&quot;, 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: &#39;File is too large&#39; }
end

When I run try to access documents in index method in library_documents_controller, in the following way:

class LibraryDocumentsController &lt; AuthenticatedController

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

it gives me the following error:

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

Moreover, on doing Document.last in rails console it gives me the following error:

NoMethodError: undefined method `before_attached_file_post_process&#39; 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&#39;

Sorry the question seems very long, but I tried my best to explain my problem.

答案1

得分: 1

我认为你应该更改 Msp::Templates::Document 模型中的代码。

从这个

validates_attachment :attached_file,
                     size: { less_than: 5.megabytes, message: &#39;File is too large&#39; }

validates :attached_file,
           size: { less_than: 5.megabytes, message: &#39;File is too large&#39; }

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

英文:

I think you should change the code in Msp::Templates::Document model.

From this

validates_attachment :attached_file,
                     size: { less_than: 5.megabytes, message: &#39;File is too large&#39; }

To

validates :attached_file,
           size: { less_than: 5.megabytes, message: &#39;File is too large&#39; }

It seems like the method before_attached_file_post_process belongs to the paperclip gem, and you are not using it anymore.

huangapple
  • 本文由 发表于 2023年4月13日 16:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定