Question

I am trying to build a query in irb for my rails project.

Test.joins(:server).where(server: { admin_user_id: 1})

which gives the following error:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: server.admin_user_id: SELECT "tests".* FROM "tests" INNER JOIN "servers" ON "servers"."id" = "tests"."server_id" WHERE "server"."admin_user_id" = 1

This seems to be complaining about not being able to find the admin_user_id column in the servers table. Now the strange thing is if I sql into that exact database and run:

select * from servers where admin_user_id = 1;

it returns the row fine.

How can I make this work properly?

Was it helpful?

Solution

There's an error because the generated SQL refers to a table server that does not exist (its servers).

You can use @RSB's answer, or if you want to keep the same hash syntax:

Test.joins(:server).where(servers: { admin_user_id: 1})

See example in the Active Record Query Interface Guide

OTHER TIPS

Try this one

Test.joins(:server).where("servers.admin_user_id =?", 1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top