Search code examples
ruby-on-railsruby-on-rails-5

How to fix ActiveRecord::AssociationTypeMismatch on has_many through relationships


I keep getting ActiveRecord::AssociationTypeMismatch while doing some many to many relationships.

I have tried changing the names one the models ie has_many :subscribers, through: :rafflecontestants, source: :users but that led me nowhere.

class Rafflecontestant < ApplicationRecord
  belongs_to :user
  belongs_to :raffle
end
class Raffle < ApplicationRecord
    validates :name, presence: true

    has_many :rafflecontestants
    has_many :users, through: :rafflecontestants
end
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :validatable

  has_many :rafflecontestants
  has_many :raffles, through: :rafflecontestants
end

and My Schema:

create_table "rafflecontestants", force: :cascade do |t|
    t.integer "user_id"
    t.integer "raffle_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["raffle_id"], name: "index_rafflecontestants_on_raffle_id"
    t.index ["user_id"], name: "index_rafflecontestants_on_user_id"
  end

  create_table "raffles", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.integer "winner"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "created_by"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "is_admin"
    t.string "authentication_token", limit: 30
    t.index ["authentication_token"], name: "index_users_on_authentication_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

Steps to reproduce in the console:

u = User.create(...) 
r= Raffle.create(..., Rafflecontestants: [u])
ActiveRecord::AssociationTypeMismatch: Rafflecontestant(#66411640) expected, got #<User id: 1, email: "[email protected]", created_at: "2019-07-11 19:54:55", updated_at: "2019-07-11 19:54:55", is_admin: false, authentication_token: "yeU3mgQQPyXqDrMBFPAY"> which is an instance of User(#73796060)

I am expecting to add users to the raffles using the Rafflecontestants table.


Solution

  • You're not quite approaching this in the correct manner.

    These two should both work, more info in the guides

    user = User.create(...)
    raffle = Raffle.create(...)
    raffle.users << user
    

    or ...

    user = User.create(...)
    raffle = Raffle.create(...)
    user.raffles << raffle
    

    If you had extra attributes on the join model Rafflecontestant you would have to do this

    user = User.create(...)
    raffle = Raffle.create(...)
    Rafflecontestant.create(user: user, raffle: raffle, some_extra_attribute: 1)