I have a table that hold values for school\_user\_id and homework associated with each school\_user as follows:

```
create_table "homework_students", force: :cascade do |t|
    t.integer  "school_user_id", limit: 4, null: false
    t.integer  "homework_id",    limit: 4, null: false
    t.datetime "completed_on"
    t.datetime "created_at",               null: false
    t.datetime "updated_at", 

```

Each school\_user is associated with an individual homework and homework has many school\_users.

I am trying to print the total school\_users connected to an individual piece of homework on my view.

In my controller:

```
@total = HomeworkStudent.where("SELECT homework_id FROM homework_students WHERE 'school_user_id = (?)'").sum(:school_user_id)

```

Models

```
homework
has_many :homework_students, :class_name => 'HomeworkStudent'

homework_students
belongs_to :homework, :class_name => 'Homework', :foreign_key => :homework_id

```

I don't think this is right in my syntax - it keeps printing 0. 

Any ideas and also nicer ways one might do this?

EDIT

Got it with this.

```
@total = HomeworkStudent.find_by_sql("SELECT homework_id FROM homework_students WHERE school_user_id GROUP BY homework_id").count

```

Still would like to know if better way.