The ActiveModel::UnknownAttributeError is not resolved in Rails.

Asked 2 years ago, Updated 2 years ago, 41 views

Prerequisites/What you want to achieve

I'm developing an application. While implementing and adjusting the notification function, the above ActiveModel::UnknownAttributeError appeared. I want to solve it.

Problems/Error Messages you are experiencing

ActiveModel::UnknownAttributeError in PostsController#create
unknown attribute 'visitor_id' for Notification.

Source Codes Affected

Review results of model notification in railsc

irb(main): 001:0>Notification.new
(0.2ms) SELECT sqlite_version(*)
TRANSACTION(0.0ms) begin transaction=>#<Notification:0x0000562096ee9d98
id: nil,
visitor_id:nil, 
visited_id:nil,
post_id:nil,
reply_id —nil,
action: '' ,
checked: false,
created_at:nil,
updated_at:nil>

migrate/create_notifications.rb

class CreateNotifications <ActiveRecord::Migration [6.1]
  def change
    create_table —notifications do | t |
      t.interger:visitor_id, null:false
      t. integer:visited_id, null:false
      t.integer:post_id
      t. integer —reply_id
      t.string:action, default:', null:false
      t. boolean: checked, default: false, null: false

      t.timestamps
    end

    add_index:notifications,:visitor_id
    add_index:notifications,:visited_id
    add_index:notifications,:post_id
    add_index:notifications,:reply_id

  end
end

Additional Information

post_create.rb

class PostsController <ApplicationController
  before_action —Authenticate_user!


  def create
    @post=Post.new(post_params)
    @post.user_id=current_user.id
    @post.sender_id=current_user.id
    @receiver=User.where.not(id:current_user.id).order(:received_at).first
    @post.receiver_id=@r eceiver.id
    @post.save!

    @post.create_notification_by(current_user)
    response_to do | format |
      format.html {redirect_to request.reference}
      format.js
    end


    rescue ActiveRecord::RecordInvalid=>e
      ppe.record.errors

    @receiver.update!(received_at::Time.now)

    redirect_to posts_path
  end


    private
        def post_params
          param.require(:post).permit(:dear,:content,:from,:sender_id,:receiver_id,:user_id)
        end
end
model/post.rb

create_notification_by (current_user)
    notification=current_user.active_notifications.new(
      post_id: self.id,
      visited_id —self.receiver_id,
      action: "post"
    )
    notification.save if notification.valid?
  end

Tried

I just tried something like the one in the article below, but it didn't work.
I'm sure, but I was the first to write in the error visitor_id, so I thought I had to fix it, so I thought I could fix it even if I checked it on the console...

unknown attribute '○○○○' for○○○.→ column rename
bin/rails db:reset does not work

Supplementary information (for example, FW/Tool Version)

ruby 3.0.2
rails 7.0.1

ruby-on-rails ruby

2022-09-30 20:24

1 Answers

The message contains a mixture of visitor_id and visitor_id (o and e).Are you sure you typed it wrong?

Also, since PostsController#create is the error, the first thing to see is the implementation in PostsController.


2022-09-30 20:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.