2

I've got a table that looks like this

enter image description here

I want to display the amount of entries by a certain agent. I know I can use e.g.

orders.sum(:demand)

to sum up the demand. (table is called orders) However, there has to be a way to sum up the entries right? E.g. for agent 1, this would be 4, for agent 2 it would be 1 and for agent 3 it would be 4 entries (as can be seen in the screenshot).

Any ideas?

Krawalla
  • 198
  • 1
  • 13
  • Possible duplicate of [Select, group and sum results from database](http://stackoverflow.com/questions/1556289/select-group-and-sum-results-from-database) – Shadow Mar 22 '17 at 15:17
  • You need to use sql's `group by` clause. How to use this in rails is described in the duplicate topic I linked. – Shadow Mar 22 '17 at 15:18

3 Answers3

2

If I understood correctly, what you need is the amount of entries by :agent_id, this should work:

Order.group(:agent_id).count

And the result is

=> {1=>4, 2=>2, 3=>3}

Edit:

If what you need is the count of the current_user:

Order.where(agent_id: @current_user).count

So if current_user is '1', you get:

=> 4

Community
  • 1
  • 1
Alejandro Montilla
  • 2,626
  • 3
  • 31
  • 35
  • this actually works as you described. I only want to display the "count" of the current user. Each user can either be an agent or a client. So user_id is not necessarily = agent_id. Anyway, each agent shall only see his own count.. – Krawalla Mar 22 '17 at 15:46
  • So what you need is, to find the number of entries by the currect user? If so, where do you have your currect user? – Alejandro Montilla Mar 22 '17 at 15:53
  • do you mean my sessions_helper? `def current_user @current_user ||= User.find_by(id: session[:user_id]) end` – Krawalla Mar 22 '17 at 15:56
  • Strange as it is, this returns "0" for any agent that i tried. – Krawalla Mar 22 '17 at 16:21
  • updated the original table, maybe this is because of the actual entries in the "order" column?? – Krawalla Mar 22 '17 at 16:26
  • That must be because your `@current_user` variable stores a user without entries, can you check the value of that varaible just before the call of you `.count`? – Alejandro Montilla Mar 22 '17 at 16:29
  • `CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "4"]] (0.2ms) SELECT COUNT(*) FROM "orders" WHERE "oreders"."agent_id" IS NULL` – Krawalla Mar 22 '17 at 16:35
  • Ok, lets try this, do you have your `.count` method call in a controller?? – Alejandro Montilla Mar 22 '17 at 16:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138752/discussion-between-alejandro-montilla-and-krawalla). – Alejandro Montilla Mar 22 '17 at 16:48
1

Assuming by 'certain', you're sending some sort of request to the controller, specifying the agent you want to query...

Order.where(agent_id: params[:agent_id]).count
JayJay
  • 746
  • 1
  • 9
  • 21
  • well this sounds pretty reasonable... however, this error occurs: `undefined local variable or method 'params' for #` – Krawalla Mar 22 '17 at 15:44
  • This is psudo code, you'll need to pass in the agent_id somehow, this could either be via the request parameters (params) from the controller, or from a variable derived by some other means: agent = 123 Order.where(agent_id: agent).count – JayJay Mar 23 '17 at 09:39
1

With activerecord you can use the count method for counting the results.

Order.count # => 9

With the group method you can group the results by another column.

Order.group(:agent_id) # => #<ActiveRecord::Relation [ ... ] > 

Together you can get the results you want:

Order.group(:agent_id).count # => {1: 4, 2: 2, 3: 3}
Lasse Sviland
  • 1,479
  • 11
  • 23
  • this actually works as you described. I only want to display the "count" of the current user. Each user can either be an agent or a client. So user_id is not necessarily = agent_id. Anyway, each agent shall only see his own count... – Krawalla Mar 22 '17 at 15:46