1

For Example, I want to know the User have how many post. So, I do it in this way;

user.posts.length

It works, but when I see the server log, it shows me that:

SELECT * FROM "posts" WHERE ("posts".user_id = 6) 

actually, I need to know the post number only, how can I do this? Thank you.

3 Answers 3

3
user.posts.count

# This will generate this:
SELECT count(*) AS count_all FROM `posts` WHERE (`posts`.user_id = 1) 
Sign up to request clarification or add additional context in comments.

Comments

1

try this:

 user.posts.count

Comments

0

You can use count/size method instead of length:

user.posts.count

It will execute query similar to:

SELECT COUNT(*) AS count_id FROM "posts" WHERE ("posts".user_id = 6) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.