ruby on rails - Why is this group clause not working -


i have payment model 5 rows. 4 of them same user_id , 1 different. try group results user_id. should return 2 results, when trying 5 results. idea why?

(i'm using postgres)

payment.find(:all, group: "user_id,id").count => 5  

also tried

payment.find(:all, group: "id,user_id").count => 5  

updated: if not add "user_id" i'm getting error

activerecord::statementinvalid: pg::error: error:  column "payments.id" must appear in group clause or used in aggregate function line 1: select "payments".* "payments"  group user_id                ^ : select "payments".* "payments"  group user_id 

updated 2: query

payment.joins(:user).select('payments.id,users.username,payments.user_id,payments.token,payments.period_start_at,payments.amount_cents,payments.currency').group("user_id") 

returns pg::error: error: column "payments.id" must appear in group clause or used in aggregate function line 1: select payments.id,users.username,payments.user_id,payments.... ^

if add "payments.id" group clause not group them user_id. idea?

well, there no way can group id, id serial unique key on table. group user_id.

updated

aparently using rails v 2.3.* or something. also, database knowledge. query in sql this:

select count(user_id), user_id payments group user_id #=> user_id | count     10        2     3         1     4         10 

this give number of payments user have. translate activerecord query, have do:

payment.count(:group => :user_id) 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -