Forum: Ruby on Rails Mysql code in Ruby on Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C88d4cab75612e2e9ec1bf2e21dcf58e?d=identicon&s=25 Naveed Alam (nyt1972)
on 2016-09-02 05:47
Pls help me to write the below code in Ruby on Rails 2.3,

select count(*) as students,  batch_id from students group by batch_id;

I want the output of counted students in a variable.

thanks.
C88d4cab75612e2e9ec1bf2e21dcf58e?d=identicon&s=25 Naveed Alam (nyt1972)
on 2016-09-03 09:17
Naveed Alam wrote in post #1184925:
> Pls help me to write the below code in Ruby on Rails 2.3,
>
> select count(*) as students,  batch_id from students group by batch_id;
>
> I want the output of counted students in a variable.
>
> thanks.

I tried this:

@total_count =  Student.all(:joins => ["INNER JOIN students on
students.batch_id = batches.id"],
      :conditions=>{:'batches.is_deleted' => false,:'batches.is_active'
=> true}).count

but it give me the following error:

Mysql::Error: Not unique table/alias: 'students': SELECT `students`.*
FROM `students` INNER JOIN students on students.batch_id = batches.id
WHERE (`batches`.`is_active` = 1 AND `batches`.`is_deleted` = 0)

Pls help.
This topic is locked and can not be replied to.