jeudi 26 octobre 2023

ActiveRecord::InverseOfAssociationNotFoundError in rails 7

Hii in my rails application when I going to open the profile page the following error comes My ruby version is "3.2.2" and my rails version is "7.0.8"

ActiveRecord::InverseOfAssociationNotFoundError in Users#show

Showing /home/nitish/Documents/Bestristey/app/views/users/_user_profile_image.html.erb where line #3 raised:

ActionView::Template::Error (Could not find the inverse association for profile_image_attachment (:record in ActiveStorage::Attachment)):

        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">

This is the code of my user model:-

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


And this is the code from my models/active_storage/attachment.rb file:-

`
class ActiveStorage::Attachment < ApplicationRecord

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

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

I have tried of using :inverse_of in the users model has_one_attached association but then it gives argument error. I also tried some other stuffs also but still no any progress.

Aucun commentaire:

Enregistrer un commentaire