class Course < ActiveRecord::Base
has_many :registrations
delegate :count, to: :registrations, prefix: true
end
class Registration < ActiveRecord::Base
belongs_to :course
end
In my Courses index view I show a count of Registrations for each record. What is best practice for this? Using includes(:registrations) with the delegated registrations_count method looks slower (in the console) than doing a database count in the main query.
I want to show all records, so I can't use an INNER join(). Using includes() as below gives the error PG::GroupingError: ERROR: column "registrations.id" must appear in the GROUP BY clause or be used in an aggregate function
I tried adding a where clause on :registrations to this but it still errored:
Course.includes(:registrations).group("courses.id").select("courses.*, COUNT(registrations.id) as registrations_count")
Is it right to have to specify the outer join as follows?
Course.joins('LEFT OUTER JOIN registrations ON registrations.course_id = courses.id').select('courses.*, COUNT(registrations.id) as registrations_count').group('courses.id')
This last query does work but it feels what I'm doing should be fairly standard so I'd like to be sure I'm taking the right approach in general.