I have three Models. Both makes
and models
table have name
column inside.
class Review < ActiveRecord::Base
belongs_to :model
end
class Model < ActiveRecord::Base
belongs_to :make
has_many :reviews
end
class Make < ActiveRecord::Base
has_many :models
end
In Active Admin on Index Page I want to sort reviews by make and model.
I figured out how to sort it by model, because it has key the reviews
table.
Tried a lot of things. Nothing works to sort by make name. I was able to list though.
ActiveAdmin.register Review do
column "Model", sortable: 'model_id' do |p|
p.model.name
end
column "Make", sortable: 'make_id' do |review|
review.model.make.name
end
end
Only sorting by model name works. I think if I add make_id
to Reviews
it will be working, but it seems redundant, cause a chain like review.model.make
perfectly works
Let's say a
review
is a score between 1 and 5. You will have to compute the average value for eachmake
.To compute an average value for a
make
, do:Then:
I advice you to create an
make_average_review(make_id, average_review)
This way you can store/update your average review for everymake
and then sort bymake