# Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678)

**Code:** `ActiveRecord::AssociationTypeMismatch` · **Category:** Ruby / Rails · **Seen on:** Ruby

You assigned the wrong kind of object (or an id string) to an association.

## Meaning

Assign records to associations (`post.author = user`) and ids to `_id` attributes (`post.author_id = 42`). Passing an id string to `author=` or a record of another class raises this. In forms use `author_id`; with strong params permit `author_id` not `author`.

## Common causes

- Assigning an id to the association setter
- Wrong model class assigned
- `params[:author]` string used for the association
- Polymorphic association type mismatch
- STI class mismatch

## Quick fix

1. `post.author_id = params[:author_id]` or `post.author = User.find(id)`
2. Permit `author_id` in strong params
3. Check the association class and STI types

## Detailed fix by platform

### Rails

1. `post.author = User.find(42)  # or post.author_id = 42`

## How to diagnose

1. **Assigned** — Record or id?
2. **Class** — Expected?
3. **Params** — author_id?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/ruby/activerecord-associationtypemismatch)
