Reputation: 1408
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?
Upvotes: 0
Views: 53
Reputation: 24340
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
Upvotes: 1
Reputation: 17834
Try this one
Test.joins(:server).where("servers.admin_user_id =?", 1)
Upvotes: 0