1

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

2
  • map returns an array of the results of the block applied, so all you're getting is an array of computed scores. What were you after originally? Commented Jul 27, 2011 at 17:11
  • I stink at this! I'm trying to compute a score based on what is returned from the query and then I want to sort the results by that computed score. I'm trying everything every which way (and wrong way). Any ideas? Your help is really appreciated. Commented Jul 27, 2011 at 17:14

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Awesome, thanks! What do l1 and l2 represent? I'm sure this works, just wondering if you could help me understand. Thanks again!
Haha you're more confident than me, please try it first. sort does a comparison sort, so l1 and l2 represents each pair of values being compared... in this case, each of your matched objects.
haha thanks - i'll give it a shot, when you mean pair of values being compared what are you referring to? do you mean it's like a step by step comparison? like it will compare the first to next and so on?
Here's a question that goes into Ruby's sort algorithms: stackoverflow.com/questions/855773/…
I think sort_by would be a better call than sort for anything expensive (like compute_score probably is).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.