开发者

Sorting query results using a model method

开发者 https://www.devze.com 2023-03-23 02:45 出处:网络
I have the following mapping (@location_matches represents a standard SQL query`: @matches = @location_matches.map { |listing|

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号