Question: how to count on unique records of public_activity actions in rails?

Question

how to count on unique records of public_activity actions in rails?

Answers 1
Added at 2017-01-03 00:01
Tags
Question

I am using public_activity gem.

@num = PublicActivity::Activity.where(owner_id: current_user.id, key: "micropost.read").count

This would show correctly how many times a current_user has read on all microposts. However, if current_user has read on the same micropost muliple times, it would count those number too. But I don't want that. If a current_user has read the same micropost multiple times, I'd like to be counted as only one time.

I have tried

@num = PublicActivity::Activity.where(owner_id: current_user.id, key: "micropost.read").distinct.count

it did not show any error messages, but it did not work.

I have tried

@num = PublicActivity::Activity.distinct.where(owner_id: current_user.id, key: "micropost.read").count

it did not work.

What is correct syntax for getting this done? Thanks in advance!

Ruby 2.3.1, rails 5.0.0.1

Answers
nr: #1 dodano: 2017-01-03 12:01

Try querying the same using trackable_id. I'am using pluck(:id) so that my querying becomes easy since it gives me an array of id's.

@num = PublicActivity::Activity.where(owner_id: current_user.id, key: "micropost.read", trackable_id: Micropost.pluck(:id)).distinct.count
Source Show
◀ Wstecz