I need to order a query of Profile entries in an ActiveRecord::Relation object by a value that is computed on run-time.
I have created and assigned a temporary column distance in my controller for the Relation object by using attr_accessor like so:
@profiles.each do |p|
p.class_eval do
attr_accessor :distance
end
end
@profiles.each do |p|
p.distance = distance_arr[@profiles.index(p)] # Distance values obtained from an instance array
end
However, when I try to order the Relation with the order method, I get a no such column: distance error i.e. it's not picking up the temporary field.
Here's how I tried to order it in my controller (note I'm using the will_paginate gem, but that is irrelevant).
@profiles = @profiles.order('distance ASC').paginate(page: params[:page])
Aucun commentaire:
Enregistrer un commentaire