Hi,

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, I thought about rendering each
campaign from a seperate action, with a view attached to it.

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

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

in the controller (output is a string). I get the feeling my whole
approach is wrong, since I cant find a solution and I've been
searching for two days already. I hope some of you can tell me how to
solve this.

Thanks in advance,
Heike

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Dave Aronson at Jan 11, 2012 at 9:30 pm

    On Tue, Jan 10, 2012 at 04:36, Heike 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] %> <!-- no results for that day -->
    <% 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 Aronson, President, Dave Aronson 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)

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 11, '12 at 10:35a
activeJan 11, '12 at 9:30p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Dave Aronson: 1 post Heike: 1 post

People

Translate

site design / logo © 2021 Grokbase