Link content
I am trying to introduce a notification function to the application referring to this article, but when I press the like button

ActiveRecord::StatementInvalid in FavoritesController#create
SQLite3::SQLException: no such table: main.visiteds: INSERT INTO "notifications" ("visitor_id", "visited_id", "micropost_id", "action", "created_at", "updated_at") VALUES (?, ?, ?) , ?, ?, ?)

There is no such table as visiteds. There is a column called visited_id in the notification model, but for some reason that column is mentioned here and I'm confused. What could be the cause in this case?

What I tried

・Rebuilding the table
· Restart the server
・I tried to create an instance with the command

pry(main)>user.active_notifications.create(visited_id: 2, micropost_id: 116, action:'like')
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.visiteds: INSERT INTO "notifications" ("visitor_id", "visited_id", "micropost_id", "action", "created_at", "updated_at") VALUES ( ?, ?, ?, ?, ?, ?)
from /Users/ryoga_h/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize'
Caused by SQLite3::SQLException: no such table: main.visiteds
from /Users/ryoga_h/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize'

And again, it says that the visiteds table cannot be found.

notifications table

create_table "notifications", force: :cascade do |t|
    t.integer "visitor_id", null: false
    t.integer "visited_id", null: false
    t.integer "micropost_id"
    t.integer "comment_id"
    t.string "action", null: false
    t.boolean "checked", default: false, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["comment_id"],

 name: "index_notifications_on_comment_id"
    t.index ["micropost_id"],

 name: "index_notifications_on_micropost_id"
    t.index ["visited_id"],

 name: "index_notifications_on_visited_id"
    t.index ["visitor_id"],name: "index_notifications_on_visitor_id"


def create_notification_like!(current_user)
    # Search for already liked
    temp = Notification.where(["visitor_id =? and visited_id =? and micropost_id =? and action =? ", current_user.id, user_id, id,'like'])
    #Create notification record only if not liked
    if temp.blank?
      notification = current_user.active_notifications.build(
        micropost_id: id,
        visited_id: user_id,
      # If i like your post, mark it as notified
      if notification.visitor_id == notification.visited_id
        notification.checked = true
      notification.save if notification.valid?

favorites controller

def create
    @micropost = Micropost.find(params[:micropost_id])
    redirect_back(fallback_location: root_url)


class Notification  {order(created_at: :desc)}
  belongs_to :visitor, class_name: "User", foreign_key: "visitor_id", optional: true
  belongs_to :visited, class_name: "User", foreign_key: "visited_id", optional: true
  belongs_to :micropost, optional: truebelongs_to :comment, optional: true


has_many :active_notifications, class_name:'Notification', foreign_key:'visitor_id', dependent: :destroy
  has_many :passive_notifications, class_name:'Notification', foreign_key:'visited_id', dependent: :destroy


class CreateNotifications</pre>
<p><br />
after removing optinoal:true</p>
<pre><code>[16] pry(main)>a = user.active_notifications.create!(action:'like', micropost_id: 100,visited_id: 4)
   (0.1ms) begin transaction
  User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ?LIMIT? [["id", 1],

 ["LIMIT", 1]]
  User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ?LIMIT? [["id", 4],

 ["LIMIT", 1]]
   (0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Miropost must exist, Comment must exist
from /Users/ryoga_h/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/activerecord-5.1.7/lib/active_record/validations.rb:78:in `raise_validation_error'

I couldn't solve it myself because of my learning and lack of self-solving ability. I would be grateful if someone could profess me.

  • Answer # 1

    in model notificationbelongs_to visitorIsn't it?
    In this case, rails interprets it as "related to Visitor model", so the above error occurs.
    Maybe it's a User, right?
    belongs_to visitor class_name:'User',foreign_key: :visitor_idPlease do something.
    visited also