I’m usually not one to jump to “the library must have a bug” when my code breaks, but after talking over some behavior I’ve been seeing with a coworker I’m not so sure.
The app is very simple. It’s just a (half baked) bookshelf manager for those IKEA grid bookshelves.
The issue has to do with querying the Book model. Book has a polymorphic has_one relationship with Item via the placeable polymorphic interface. During development, I accidentally tried to filter Books by an Item id. I did:
Book.where(item: 14)
which generated
SELECT "books".* FROM "books" WHERE "books"."id" = $1 [["id", 14]]
This does not do what it should. It loads books with the id 14, not books associated with items with id 14. Basically I expected it to generate the same SQL as this (which is what I meant to write):
Book.where(item: Item.find_by(id: 14))
which generates
SELECT "items".* FROM "items" WHERE "items"."id" = $1 ORDER BY "items"."index" ASC LIMIT $2 [["id", 14], ["LIMIT", 1]]
SELECT "books".* FROM "books" WHERE "books"."id" = $1 [["id", 11]]
So, my question is, obviously my first query was nonstandard/nonsense, but with that in mind, shouldn’t it have either generated the same SQL as the intended query or errored out? Isn’t the generated SQL just wrong?
For those interested in messing with my repo, here it is: GitHub - AHBruns/buk. It uses Figaro for all configuration. You’ll need an application.yml
like this to get things working:
development:
# db
database_name: "buk_dev"
database_username: ""
database_password: ""
# misc
jwt_secret: "development_jwt_secret"
google_books_api_key: "<you gotta provide this>"
test:
# db
database_name: "buk_test"
database_username: ""
database_password: ""
# misc
jwt_secret: "testing_jwt_secret"
google_books_api_key: "<you gotta provide this>"
production:
# todo