I’ve populated a hash with two different models. I then try to sort them like so:
@search_results = User.find(:all, :conditions => ['name LIKE ?', "%#{params[:query]}%"])
@search_results += Book.find(:all, :conditions => ['title LIKE ?', "%#{params[:query]}%"])
@search_results.sort! { |a,b| a.impressions_count <=> b.impressions_count }
This throws the following error:
comparison of User with Book failed
Both users and books have an integer-based impressions_count. Why can’t I sort via this attribute? What other options do I have?
I faced a similar problem recently and ended up writing some custom sql because all other ways returned an array. Pretty sure its not a good idea to use the sort method since it will always be more efficient to sort in SQL than ruby, especially when the data set gets large
The above is completely untested code, more of an example than anything