Reputation: 26919
Here is the classes I have:
Model Organization
has_many Students
Model Student
has_many Classes
belongs_to Organization
Model Class
a field named : price
belongs_to Student
scope :top_expensive_classes, joins(:students).order('price DESC')
Now I want to list the top 10 expensive classes
At least the first problem I have is that in the params
I have the organization_id
to filter based on that But I write my controller like this which does NOT work because it thinks it should find organization_id in the Class
model but it is in the Student
model.
@results = Class.top_expensive_classes.where(organization_id: params[:id]).limit(RESULT_SET_COUNT)
So I was wondering if there is a way to fix this? I think I should introduce a new join somewhere? but couldn't figure it out.
Upvotes: 0
Views: 63
Reputation: 4555
There's a typo in your scope: joins:(:programs)
should be joins(:programs)
To fetch based on the organization id in Student you may be able to do this:
@results = Class.top_expensive_classes
.joins(student: :organization)
.where(organization: {id: params[:id]})
Upvotes: 1