Hi Jeremy,
thx for reply! I wasn't aware about select_append block array possibility...
One more question related to joined tables/models to you. How can i access
attributes(columns) from joined table?
User.id works for primary table, for secondary (joined) table or calculated
attributes (columns)? I'm using hash syntax User[:cals] for calculated
columns. Is this correct way?
How can i access column from joined table, ie. "daily_logs.item"?
Thank you
On Sunday, December 20, 2015 at 8:36:09 PM UTC+1, Jeremy Evans wrote:
>
> On Sunday, December 20, 2015 at 11:27:20 AM UTC-8, Happy wrote:
>>
>> Hi,
>> please, can you suggest how to to add count on grouped dataset using
>> models?
>>
>> This
>> User.join(DailyLog,:user_id=>:id).group_and_count(:user_id).select_all.
>> select_append{sum(sum).as(cals)}
>>
>> translates to:
>> SELECT *, sum(`sum`) AS 'cals' FROM `users` INNER JOIN `daily_logs` ON (
>> `daily_logs`.`user_id` = `users`.`id`) GROUP BY `user_id`
>>
>>
>> and im trying to achieve:
>> SELECT *, sum(`sum`), count(*) AS 'cals' FROM `users` INNER JOIN
>> `daily_logs` ON (`daily_logs`.`user_id` = `users`.`id`) GROUP BY
>> `user_id`
>>
>> Thank you
>>
>
> Here you go:
>
> DB[:users].
> join(:daily_logs, :user_id=>:id).
> group(:user_id).
> select_append{[sum(:sum), count{}.*.as(:cals)]}
>
> Thanks,
> Jeremy
>
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.