Home>

Premises /What you want to achieve

We are developing an application. Among them, the above ActiveModel :: UnknownAttributeError appeared while implementing and adjusting the notification function. I want to eliminate it.

Problems /error messages occurring

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

Corresponding source code

Confirmation result of model Notification with
rails c
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.integer: 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 end
    add_index: notifications,: visitor_id
    add_index: notifications,: visiteded_id
    add_index: notifications,: post_id
    add_index: notifications,: reply_id
  end end
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= @ receiver.id
    @ post.save!
    @ post.create_notification_by (current_user)
    respond_to do | format |
      format.html {redirect_to request.referrer}
      format.js
    end end
    rescue ActiveRecord :: RecordInvalid= >
 e
      pp e.record.errors
    @ receiver.update! (received_at :: Time.now)
    redirect_to posts_path
  end end
    private
        def post_params
          params.require (: post) .permit (: dear,: content,: from,: sender_id,: receiver_id,: user_id)
        end end
end end
model /post.rb
def 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 end

I tried it

I tried something like the one in the following article, but it doesn't work.
Certainly, I did the writing in the error visiter_id first. So I have to fix it! I thought I'd fixed it, and even if I actually checked it on the console, I thought I could fix it. .. ..

unknown attribute'○○○○' for ○○○. → Rename column
bin /rails db: reset doesn't work

Supplementary information (FW /tool version, etc.)

ruby ​​3.0.2
rails 7.0.1

  • Answer # 1

    In the messagevisitor_idWhenvisiter_idAre mixed (o and e). Isn't it wrong?

    Also, the error is occurringPostsController # createSo the first thing to seePostsControllerIt is an implementation in.

    Thank you for your comment, I also added a post controller and a model.

    sasaharukimedes2022-01-25 22:51:13

    Probably ahead of @ post.create_notification_by (current_user). Read the call stack that you may see in the error message.

    nekketsuuu2022-01-25 22:51:13