Chiperific
Chiperific

Reputation: 4686

ruby iteration based on table values

The project:

I'm creating a dynamic reporting system.

Metrics define the purpose of the reported data. For example:

"13 house fires."

The admin will define "house fires" as a metric through one form, the reporters will simply add the "13" through a different form.

However, there's another level: I want the reporting to be verbose across connected data points:

"13 house fires during January affecting 42 individuals (or 16 families) "

The verbage is stored in a table "metrics", the data is stored in a table "metrics_data"

Here's the metrics table from the sample above:

metric_id | parentID | childID | prefix       | suffix      | program_id
    1     |     1    |   1     |              | house fires |    1
    2     |     1    |   2     | during       |             |    1
    3     |     1    |   3     | affecting    | individuals |    1
    4     |     1    |   4     | (or          | families    |    1

The key for the sentence-based organization is the parentID - childID relationship.

Here's the metrics_data table:

metric_id | value |     date
    1     |  13   |   01/01/12
    2     |  nil  |   01/01/12
    3     |  42   |   01/01/12
    4     |  16   |   01/01/12

The goal:

I want to organize the view (looping through the parentID) to show the metric verbosely:

Program #1

(parentID: 1): "# house fires during (date) affecting # individuals (or # families) "

(parentID: 2): "# shelters during (date) providing # overnight stays (for # individuals) "

Program #2

(parentID: 1): "# new volunteers recruited during (date) "

(parentID: 2): "# volunteers served # hours during (date) "

The code:

class Program < ActiveRecord::Base
  has_many :programs_metrics
  has_many :metrics, through: :programs_metrics
end

class Metric < ActiveRecord::Base
  has_many :programs_metrics
  has_many :metrics, through: :programs_metrics
end

Partial _program.html.erb (for programs/index.html.erb):

<% program.metrics.each do |metrics| %>
  <div class="row offset1">
   <% program.metrics.each do |pid| %> #how do I loop here based on parentID, sorted by childID?
     <%= pid.prefix %>
     #
     <%= pid.suffix %>
    <% end %>
  </div>
<% end %>

I know I could split this out into a separate table and define the relationship between parentID and childID between two tables, but it seems overly complex to add another relationship layer.

Upvotes: 1

Views: 74

Answers (1)

Vox
Vox

Reputation: 178

You should probably treat the "parent" and "child" elements like attributes to Metrics, rather than trying to reuse the same model. An alternative would be to create another model called "Events" or whatever, and these Events are related to programs, and Metrics are related to Events. I would probably create a "Metric Type" as well instead of having a prefix or suffix associated directly with each record.

I think that trying to treat all the Metrics at the same level, and trying to loop through them with parentID and childID to create a sentence is infinitely more complicated than just adding another layer to the relationship.

Hopefully this is enough of a push...

Upvotes: 1

Related Questions