models with has_and_belongs_to_many association showing Object doesn't support #inspect

Viewed 138

I have two models that I would like to join with a many-to-many relationship.

class Klass < ApplicationRecord
    has_and_belongs_to_many :users
end

class User < ApplicationRecord
  has_and_belongs_to_many :klasses
end

I also created a join table for the two tables which looks like this

class CreateKlassesSchoolsJoinTable < ActiveRecord::Migration[6.1]
  def change
    create_table :klasses_users, id: false do |t|
      t.belongs_to :klass
      t.belongs_to :user
    end
  end
end

When I try to create a record with an association like this

u = User.create!()
k = Klass.create!()
u.klasses << k

I get:

Klass Load (0.3ms)  SELECT "klasses".* FROM "klasses" ORDER BY "klasses"."id" ASC LIMIT $1  [["LIMIT", 1]]
  TRANSACTION (0.1ms)  BEGIN
  User::HABTM_Klasses Create (0.6ms)  INSERT INTO "klasses_users" ("klass_id", "user_id") VALUES ($1, $2)  [["klass_id", 76], ["user_id", 732]]
  TRANSACTION (1.9ms)  COMMIT
(Object doesn't support #inspect)

Can somebody please explain to me what the error (Object doesn't support #inspect) means, and what am I doing wrong in trying to create a many-to-many relationship between these two models? Thanks!

0 Answers
Related