On Mar 16, 8:33 am, Michael Pavling <[email protected]> wrote:
>
> And post again if you need any more help.

Hey, thanks for the help so far. I went with a method in the
Medicine.rb model file and I'm trying to display the results through
the view, using code similar to what you suggested. I modified the
code to appear as follows for now:

def grouped_stocks
  stocks.group_by{|s| [s.route.name, s.strength]}
end

The result is an ordered hash, correct? Here's some really ugly code
in the view:
<%=h "#{s.first}" %>
This prints to screen as a concatenation of the two values in they key
of the hash item, like:

Oral Suspension100mg/5mL

There's no space between the two parts of the key.

So I tried this:

<%=h "#{s.first.first}" %>

This returns just the route name, which is good for display:
Oral Suspension

To return just the strength, I put this in place

<%=h "#{s.first.last}" %>

Which returns:
100mg/5mL

This strikes me as the wrong way to access these parts of the hash. Is
there a better way to do this?

What is the syntax for extracting a single component of the value part
of this hash? Let's say that I wanted to extract the strength from the
value part of the hash instead of the key part of the hash (since it
occurs in both).

I tried the following:

<%=h "#{s.last(:strength)}" %>
<%=h "#{s.last.first}" %>

and a few more, but since the value of the hash is an object, I always
see something like:
#<Stock:0x1032938e0>

What's the proper way to access attributes of the object that is
returned as the value of the hash?

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)}" %>

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