On Tue, Jan 10, 2012 at 04:36, Heike [email protected] wrote:
I’ve got a bunch of campaigns I wish to render into an index-page.
Two diagrams have to be in each campaign (jQuery). One of them
requires a database query within a loop (the finished orders for each
day of one week).
Since its bad code to put a database query in the view, and I dont
want to work with 4 dimensional arrays,
Frankly I think the multi-dimensional arrays (or rather, hashes) will
be the way to go. BTW, if I grok in fullness, you need sets of
results by campaign and weekday – if only one diagram needs them (as
implied above), you’ve got three dimensions (campaign, weekday, and
whatever you want to organize within that by).
In the controller I’d do something like:
@results = {}
db_rows.each do |row|
@results[row.campaign_id] ||= []
@results[row.campaign_id][row.weekday] ||= []
@results[row.campaign_id][row.weekday] << row
end
Then in your view you can do something like:
<% @results.keys.each do |campaign_id, campaign_results| %>
<% campaign_results.each_index do |day_num| %>
<% next if ! campaign_results[day_num] %>
<% campaign_results[day_num].each do |result| %>
<% end %>
<% end %>
<% end %>
There may be variations needed if your weekdays are named rather than
numbered, etc.
But I either get format errors on Nil if I try
<% @campaigns.each do |c| %>
<%= render :action => “get_single_campaign”, :params => {:campaign_id
=> c.id} %>
<% end %>
Which part is giving you errors, and what exactly is the error? The
error message should tell you what line is causing problems.
or with no html at all if I’m using
@get_all_campaign_output = “”
@campaigns.each do |c|
@get_all_campaign_output += get_single_campaign(c.id)
end
Composition by string concatenation is going to be slow slow slow with
a decent-sized result set, even if it works. When faced with
something like that, the usual alternative is to append them all onto
an array, and join it when you need the result.
-Dave
–
Dave A., President, Dave A. Software Engineering and Training
Ruby on Rails Freelancing (Northern Virginia, Washington DC, or Remote)
DaveAronson.com, Codosaur.us, Dare2XL.com, & RecruitingRants.com (NEW!)
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (me)