Sorting query results using a model method
I have the following mapping (@location_matches
represents a standard SQL query`:
@matches = @location_matches.map { |listing|
}.compact
When I go to my view, I can access @matches.username, etc, etc, etc.
However when I add this model method, I lose all access to any of the info!
@matches = @location_matches.map { |listing|
listing.compute_score(current_user)
}.compact
开发者_开发问答
any ideas why? is there a better way to do this? thank ya
Here's sorting by computed score:
@matches = @location_matches.sort_by{|location| location.compute_score(current_user)}
http://www.ruby-doc.org/core/classes/Enumerable.html#M001481
Put .reverse
at the end if you want it reverse-sorted.
精彩评论