Hi, I'm pretty much a Rails newbie here...
I put together a very simple pharmaceutical inventory system for a
field hospital in Haiti. When we first started, there was nothing but
a list of medicines that were known to be on site. Now, a group
started a robust inventory and I need to present the results
differently.
I have the following tables and fields:
Model: Medicine
Table: medicines
Fields, id, name
has_many :stocks
Model: Stock
Table: stocks
Fields: id, medicine_id, route_id, strength, amount_received,
amount_dispensed
belongs_to :medicine
belongs_to :route
Model: Route
Table: routes
Fields: id, name
has_many :stocks
The "stocks" table is used as a pharmacy log -- people check out
medicines that they take for patients and check in medicines that
arrive as donations. I currently have a method in the Medicine model
that looks like this:
def amount_on_hand
stocks.to_a.sum { |rec| (rec.amount_received -
rec.amount_dispensed)}
end
On the page that lists all of the medicines, I call
medicine.amount_on_hand to display a generic number of how many units
of that type of medicine are on hand.
...
Ibuprofen -- 500
Morphine -- 1000
etc....
There now are over 700 types of medicine on hand and many of them come
in a variety of routes and strengths. Routes are: oral tablet, oral
suspension, IV, intramuscular, etc.... Strengths are: 50mg, 20mg/dL,
etc.... The strength field in the stocks table is a text field. There
are so many different strengths that it wasn't feasible to standardize
that at the outset.
Now, I need to group the stocks that relate to each medicine by Route
and then try to perform aggregate sums based on the strength. In other
words, it might look like:
Ibuprofen
-- Oral Tablet -- 200 mg -- 1000
-- Oral Tablet -- 500 mg -- 200
-- Oral Suspension -- 50 mg/dL -- 45
Morphine
-- Inject -- 2mg/dL -- 450
-- Inject -- 4mg/dL -- 800
etc...
I've been reading on the group_by() method for enumeration, but I
don't know how to take the results returned (ordered hash, right?) and
perform the summary methods on them. My guess is that I need to write
another method in the Medicine model that iterates through the
'stocks' table to generate the results for each different medicine
type -- but I'm having trouble with this.
Any help with this method or suggestions for alternative approaches
would be appreciated! I'd like to get the system updated as soon as I
can. Thanks!