Add data to Post query


(Andrew Byrne) #1

ok so I got a lovely query from @tgxworld some time ago.

posts = Post.where('user_id IN (?) OR topic_id IN (?) OR raw LIKE (?)', uid, tid, '%@everyone%' )
			 			.order(created_at: :desc)
			                 	.limit(params["total"])
						.offset(params["start"])

and it is quite useful, but I ain’t sure how to go adding more data to this.
What I’m really looking for is a request like above that returns:

  • username
  • avatar_template
  • like_count

pretty much what you would find in:

https://meta.discourse.org/t/19157/1.json

Any Ideas?


(Erick Guan) #2

They are populated by PostSerializer. If you use that serializer, they will be already there.

like_count for example is included in actions.


(Andrew Byrne) #3

Thank you great pointer!
My final code ended up looking like:

		posts = Post.where('user_id IN (?) OR topic_id IN (?) OR raw LIKE (?)', uid, tid, '%@everyone%' )
		 			.order(created_at: :desc)
		                 	.limit(params["total"])
					.offset(params["start"])

		# that cleaned that up 
		render_json_dump(serialize_data(posts, PostSerializer))

And the resulting object contained all that I needed

actions_summary: Array[7],
admin:true,
avatar_template:blah/blah.jpg
etc....