Mysql code in Ruby on Rails


#1

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.


#2

Naveed A. 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.