On Mar 16, 12:45 pm, Michael Pavling <[email protected]> wrote:
> On 16 March 2010 15:57, Michael Pavling <[email protected]> wrote:
>
> >> Also, is it possible to order the hash so that they appear in
> >> descending order of how much of each item is in stock? In other words,
> >> sort descending by the value that the following generates?
>
> >> <%=h "#{s.last.sum(&:amount_received) -
> >> s.last.sum(&:amount_dispensed)}" %>
>
> how about:
>
> <%
> @medicine.grouped_stocks.each_pair do |k, v|
>  v.sort_by {|stock| stock.amount_on_hand}.each do |stock|
>    %>
>    <%= "#{stock.medicine.name} #{stock.strength} #{stock.route.name}
> #{stock.amount_on_hand}" %>
>    <%
>  end
> end
> %>
>
> not sure if that will sort ascending or descending though... if it's
> the wrong way round, you can always reverse before you .each...

That makes sense -- the only issue is that amount_on_hand is a method
in the Medicine model, not in the Stock model -- it performs an
aggregate sum for all of the stocks belonging to the medicine in
question. This code uses it as a Stock method, doesn't it?

I can add a version of it to the Stock model. How would that look?
This is the version in the Medicine model:

def amount_on_hand
    stocks.to_a.sum { |rec| (rec.amount_received -
rec.amount_dispensed)}
end

-- 
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