sprite wrote:
> Right now I am wasting a query and also losing the order of Users. Was
> wondering what t he proper way to do this query is?
> 
> @top_tipper_ids = Tip.find_by_sql("SELECT SUM(tips.amount_cents)
> total_tip, tips.client_id FROM tips WHERE tips.vendor_id = #[email protected]}
> GROUP BY tips.client_id ORDER BY total_tip").collect {|e| e.client_id}
> 
> @top_tippers = User.find_all_by_id(@top_tipper_ids)
> 
> Trying to find the top tippers (Users) for a specific vendor.

Will the Calculations module help you here?

If not, you could add another join to get the user info in one query.

And never -- but never -- interpolate the user ID in the string the way 
you're doing.  Use placeholders, or you leave yourself open for SQL 
injection.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to