The proper solution is using well-designed GraphQL-to-PostgreSQL software like Hasura or PostGraphile.
There's no reason to use Rails for that (or, well, use it for anything at all, given the terrible language it's based on and its outdated architecture).