3 ms·
For a toy project I used Postgres to generate JSON objects for me and a helper function in ruby (db_helper) that parses the JSON into ruby data structures so th
by taffer 7y ago
For a toy project I used Postgres to generate JSON objects for me and a helper function in ruby (db_helper) that parses the JSON into ruby data structures so that the OP example can be written with much less boilerplate code as:
def db_all_posts_in_tag(tag_id)
db_helper("
SELECT json_agg(r)
FROM (
SELECT post.post_id, post.published, post.title, post.content
FROM post
INNER JOIN post_tag ON post.post_id = post_tag.post_id
WHERE post_tag.tag_id = $1
) AS r;
", [tag_id])
end