Rails Minitest Has Many And Belongs To Model Test ActiveRecord::UnknownPrimaryKey Error

captainawesomedi profile image Di Wu ・1 min read

When I try to run minitest with simpleCov, I want the join table model UserMessages to be covered.

I have setup fixture, to run test and keep getting an ActiveRecord::UnknownPrimaryKey error. This is expected since I don't have a primary key on the UserMessage table.

Is there a way to…


Editor guide

I don't think this has much to do with minitest, it has more to do with Rails fixtures.

You need explicit IDs if you want HABTM relations to work with those: api.rubyonrails.org/classes/Active...

Hope I understood correctly and that this helps!


I figured it out myself, I dont need fixture to do it.



You might want to share the solution, for future readers :)

I have posted on stackoverflow haha


Please don't just post a link to Stack Overflow, at least provide a summary.