1

I am trying to save an event with a name and a creator_id: 1 (which exists) but am getting this error and I can't find any references online, can someone see what's wrong with my model? Users have events, and events belongs to user as creator.

class User < ApplicationRecord
  has_many :activities, through: :Registrations ,source: Event
  has_many :events,foreign_key: :creator_id
end
    class Event < ApplicationRecord
      has_many :attendees,through: :Registrations ,source: User
      belongs_to :creator, class_name: "User"
    end
ActiveRecord::Schema.define(version: 2020_04_15_153227) do

  create_table "events", force: :cascade do |t|
    t.integer "creator_id"
    t.string "name"
    t.string "date"
    t.string "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["creator_id"], name: "index_events_on_creator_id"
  end

  create_table "registrations", force: :cascade do |t|
    t.integer "user_id"
    t.integer "event_id"
    t.string "note"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["event_id"], name: "index_registrations_on_event_id"
    t.index ["user_id"], name: "index_registrations_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end
irb(main):008:0> e
    => #<Event id: nil, creator_id: 1, name: "johns wedding", date: nil, description: nil, created_at: nil, updated_at: nil>
**irb(main):010:0> e.save**
  **(0.2ms)  begin transaction
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Event Create (1.8ms)  INSERT INTO "events" ("creator_id", "name", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["creator_id", 1], ["name", "johns wedding"], ["created_at", "2020-04-16 01:37:00.514717"], ["updated_at", "2020-04-16 01:37:00.514717"]]
   **(0.1ms)  rollback transaction**
Traceback (most recent call last):
        1: from (irb):10

> **ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.creators: INSERT INTO "events" ("creator_id", "name",
> "created_at", "updated_at") VALUES (?, ?, ?, ?)) irb(main):011:0>****
DannyCode
  • 21
  • 3

2 Answers2

1

The issue was in one of the migrations files.. i used references 'creator' in the events table migration and it was causing the issue. rolled back the database and change the events table migration to this =>

create_table "events", force: :cascade do |t|
  t.integer "creator_id"
  t.string "name"
  t.string "date"
  t.string "description"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end
DannyCode
  • 21
  • 3
0

try with setting an :inverse_of option for association.

has_many :events, foreign_key: :creator_id, inverse_of: : creator
...
belongs_to :creator, class_name: "User", inverse_of: :events
blazpie
  • 312
  • 1
  • 12
  • I just tried with the inverse options but it didn't fix the error, still getting `ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.creators: INSERT INTO "events" ("creator_id", "name", "date", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?)):` – DannyCode Apr 16 '20 at 13:53