ActiveRecord::AssociationTypeMismatch Ruby / Rails Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

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

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

Exact message you may see ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678)User expected, got Stringexpected, got which is an instance ofAssociationTypeMismatch Rails association
Seen on: Ruby

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?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

A server is already running. Check /tmp/pids/server.pid — Rails: A server is already running (pid: 12345, file: /app/tmp/pids/server.pid). Exiting (Ruby / Rails)A server is already r…AbstractController::DoubleRenderError — Rails: AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action) (Ruby / Rails)AbstractController::D…ActionController::InvalidAuthenticityToken — Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity) (Ruby / Rails)ActionController::Inv…ActionController::ParameterMissing: param is missing or the value is empty — Rails: ActionController::ParameterMissing: param is missing or the value is empty: user (Ruby / Rails)ActionController::Par…ActionController::RoutingError: No route matches — Rails: ActionController::RoutingError (No route matches [GET] "/users/new") (Ruby / Rails)ActionController::Rou…ActionController::UnknownFormat — Rails: ActionController::UnknownFormat — UsersController#show is missing a template for this request format and variant (Ruby / Rails)ActionController::Unk…ActionController::UrlGenerationError: No route matches {action...} missing required keys — Rails: ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id] (Ruby / Rails)ActionController::Url…ActionMailer: SMTP From address may not be blank / Net::SMTPAuthenticationError — Rails: ArgumentError: SMTP From address may not be blank: nil / Net::SMTPAuthenticationError: 535 5.7.8 Username and Password not accepted (Ruby / Rails)ActionMailer: SMTP Fr…ActiveRec…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.