rails 7 中的 ActiveRecord::InverseOfAssociationNotFoundError

ActiveRecord::InverseOfAssociationNotFoundError in rails 7

提问人:nitish 提问时间:10/26/2023 最后编辑:nitish 更新时间:10/27/2023 访问量:42

问:

嗨,当我要打开个人资料页面时,在我的 rails 应用程序中出现以下错误 我的 ruby 版本是“3.2.2”,我的 rails 版本是“7.0.8”

ActiveRecord::InverseOfAssociationNotFoundError in Users#show

显示 /home/nitish/Documents/Bestristey/app/views/users/_user_profile_image.html.erb,其中第 #3 行升起:

ActionView::Template::Error(找不到 profile_image_attachment 的反向关联(ActiveStorage::Attachment 中的 :record)):

        1: <div id="profile_image">
        2:     <div>
        3:         <% if user.profile_image.attached? %>
        4:         <%= image_tag(user.profile_image, class: "d-block ui-w-80 " ) %>
        5:         <% else %>
        6:         <img src ="/assets/dummy profile.jpg" alt class="d-block ui-w-80 rounded-circle">

这是我的用户模型的代码:-

class User < ApplicationRecord

      after_create :after_confirmation
  
      devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :confirmable, :trackable
            
      validates :username, presence: true, uniqueness: true

      has_one_attached :profile_image

      attr_accessor :login
  
      def login
        @login || self.username || self.email
      end
     end


这是我的models/active_storage/attachment.rb文件中的代码:-

`
class ActiveStorage::Attachment < ApplicationRecord

        belongs_to :blob, class_name: "ActiveStorage::Blob"

    
  
        def self.ransackable_attributes(auth_object = nil)
          ["blob_id", "created_at", "id", "name", "record_id", "record_type"]
        end
  
      end

我尝试在用户模型has_one_attached关联中使用 :inverse_of,但随后它给出了参数错误。我也尝试了一些其他的东西,但仍然没有任何进展。

Ruby-on-Rails Ruby-on-Rails-4 RubyGems Ruby-on-Rails-7

评论

1赞 crodev 10/27/2023
为什么一定要具体定义它?我经常使用ActiveStorage,从来没有碰过这样的东西。
0赞 nitish 10/27/2023
实际上,我试图通过使用它来解决错误(inverse_of)。如果不使用它,将保持相同的错误
0赞 nitish 10/27/2023
@crodev我特别定义了这一点,因为我使用的是 activeadmin 和“使用 Active Storage 创建 Active Admin 时,我遇到了搜索错误。为了解决这个问题,我在我的模型 user.rb 中定义了可掠夺的方法“这就是我在目录中创建附件模型active_storage的原因

答: 暂无答案