Get Post Id's of posts that a user liked


(Ryan Odening) #1

I would like to get the id’s of posts that a specific user has liked. How can I do this?


(Simon Cossar) #2

You can find that with the Data Explorer Plugin with a query like this:

-- [params]
-- string :username

WITH sample_user AS (
SELECT
u.id
FROM users u
WHERE u.username = :username
)

SELECT
p.topic_id,
pa.post_id,
pa.created_at AS liked_at
FROM post_actions pa
JOIN posts p
ON p.id = pa.post_id
WHERE pa.post_action_type_id = 2
AND pa.user_id = (SELECT id FROM sample_user)
ORDER BY pa.created_at DESC

If you need to see the actual id, instead of a link to the topic and an excerpt of the post, change the line

pa.post_id

to

pa.post_id AS id

To get the post_ids with an ActiveRecord query, do something like this, with the user_id of the user you are looking for:

PostAction.where(post_action_type_id: 2, user_id: 1).pluck(:post_id)

(Ryan Odening) #3

this looks really cool!


(Ryan Odening) #4

Specifically, I was wondering if there was a way to get this through api


(Simon Cossar) #5

You can query the Data Explorer through the API. Take a look at this post: Data Explorer Plugin.