Adding map and/or reject to a reduce in rails view












0















I am calculating an ROI value that averages over a category.



<% @categories.each do |category| %>       

<h3><strong><%= category.name %><strong></h3>
<% @category_products = category.products_by_warehouse_id(params[:id]) %>

<!-- add map/reject below -->
<%@ROI = (@category_products.reduce(0.0) {|acc, item| acc + (item.heritable_sale_price.to_f * item.product_selections.length) / item.purchase_price.to_f }) %>

<p> Category ROI: <%= number_with_precision((@ROI / @category_products.length.to_f), precision:2) %></p>

.....(close tags)......


The value throws NaN when financial data is missing. For individual values, this is fine; however, it does the same for averages with missing data as well.



How can I add map/reject into my call to throw out nil values, and get the average of what's available?



@category_products.length.to_f would also have to skip over empty elements in the array if I go this route as well, to keep the sum and length consistent. Something like .where(purchase_price: [!nil, ""]).size may work.










share|improve this question




















  • 1





    You can try using where.not to query your category_products: where.not(heritable_sale_price: nil, product_selections: nil, purchase_price: nil). If that doesn't fix your problem, any second method called within the reduce block on the item variable is able to throw a NoMethodError.

    – Sebastian Palma
    Nov 26 '18 at 2:09











  • If I take :purchase_price alone (:product_selections is a joined class, and sale price is a method) this works, and set up a second instance of <% @category_products_count = category.products_by_warehouse_id(params[:id]).where.not(purchase_price: nil).size%> in order to have consistent values for averaging. Submit as an answer?

    – Boucherie
    Nov 26 '18 at 18:11


















0















I am calculating an ROI value that averages over a category.



<% @categories.each do |category| %>       

<h3><strong><%= category.name %><strong></h3>
<% @category_products = category.products_by_warehouse_id(params[:id]) %>

<!-- add map/reject below -->
<%@ROI = (@category_products.reduce(0.0) {|acc, item| acc + (item.heritable_sale_price.to_f * item.product_selections.length) / item.purchase_price.to_f }) %>

<p> Category ROI: <%= number_with_precision((@ROI / @category_products.length.to_f), precision:2) %></p>

.....(close tags)......


The value throws NaN when financial data is missing. For individual values, this is fine; however, it does the same for averages with missing data as well.



How can I add map/reject into my call to throw out nil values, and get the average of what's available?



@category_products.length.to_f would also have to skip over empty elements in the array if I go this route as well, to keep the sum and length consistent. Something like .where(purchase_price: [!nil, ""]).size may work.










share|improve this question




















  • 1





    You can try using where.not to query your category_products: where.not(heritable_sale_price: nil, product_selections: nil, purchase_price: nil). If that doesn't fix your problem, any second method called within the reduce block on the item variable is able to throw a NoMethodError.

    – Sebastian Palma
    Nov 26 '18 at 2:09











  • If I take :purchase_price alone (:product_selections is a joined class, and sale price is a method) this works, and set up a second instance of <% @category_products_count = category.products_by_warehouse_id(params[:id]).where.not(purchase_price: nil).size%> in order to have consistent values for averaging. Submit as an answer?

    – Boucherie
    Nov 26 '18 at 18:11
















0












0








0








I am calculating an ROI value that averages over a category.



<% @categories.each do |category| %>       

<h3><strong><%= category.name %><strong></h3>
<% @category_products = category.products_by_warehouse_id(params[:id]) %>

<!-- add map/reject below -->
<%@ROI = (@category_products.reduce(0.0) {|acc, item| acc + (item.heritable_sale_price.to_f * item.product_selections.length) / item.purchase_price.to_f }) %>

<p> Category ROI: <%= number_with_precision((@ROI / @category_products.length.to_f), precision:2) %></p>

.....(close tags)......


The value throws NaN when financial data is missing. For individual values, this is fine; however, it does the same for averages with missing data as well.



How can I add map/reject into my call to throw out nil values, and get the average of what's available?



@category_products.length.to_f would also have to skip over empty elements in the array if I go this route as well, to keep the sum and length consistent. Something like .where(purchase_price: [!nil, ""]).size may work.










share|improve this question
















I am calculating an ROI value that averages over a category.



<% @categories.each do |category| %>       

<h3><strong><%= category.name %><strong></h3>
<% @category_products = category.products_by_warehouse_id(params[:id]) %>

<!-- add map/reject below -->
<%@ROI = (@category_products.reduce(0.0) {|acc, item| acc + (item.heritable_sale_price.to_f * item.product_selections.length) / item.purchase_price.to_f }) %>

<p> Category ROI: <%= number_with_precision((@ROI / @category_products.length.to_f), precision:2) %></p>

.....(close tags)......


The value throws NaN when financial data is missing. For individual values, this is fine; however, it does the same for averages with missing data as well.



How can I add map/reject into my call to throw out nil values, and get the average of what's available?



@category_products.length.to_f would also have to skip over empty elements in the array if I go this route as well, to keep the sum and length consistent. Something like .where(purchase_price: [!nil, ""]).size may work.







ruby-on-rails ruby oop view






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 8:56









sawa

132k29206308




132k29206308










asked Nov 26 '18 at 1:47









BoucherieBoucherie

315213




315213








  • 1





    You can try using where.not to query your category_products: where.not(heritable_sale_price: nil, product_selections: nil, purchase_price: nil). If that doesn't fix your problem, any second method called within the reduce block on the item variable is able to throw a NoMethodError.

    – Sebastian Palma
    Nov 26 '18 at 2:09











  • If I take :purchase_price alone (:product_selections is a joined class, and sale price is a method) this works, and set up a second instance of <% @category_products_count = category.products_by_warehouse_id(params[:id]).where.not(purchase_price: nil).size%> in order to have consistent values for averaging. Submit as an answer?

    – Boucherie
    Nov 26 '18 at 18:11
















  • 1





    You can try using where.not to query your category_products: where.not(heritable_sale_price: nil, product_selections: nil, purchase_price: nil). If that doesn't fix your problem, any second method called within the reduce block on the item variable is able to throw a NoMethodError.

    – Sebastian Palma
    Nov 26 '18 at 2:09











  • If I take :purchase_price alone (:product_selections is a joined class, and sale price is a method) this works, and set up a second instance of <% @category_products_count = category.products_by_warehouse_id(params[:id]).where.not(purchase_price: nil).size%> in order to have consistent values for averaging. Submit as an answer?

    – Boucherie
    Nov 26 '18 at 18:11










1




1





You can try using where.not to query your category_products: where.not(heritable_sale_price: nil, product_selections: nil, purchase_price: nil). If that doesn't fix your problem, any second method called within the reduce block on the item variable is able to throw a NoMethodError.

– Sebastian Palma
Nov 26 '18 at 2:09





You can try using where.not to query your category_products: where.not(heritable_sale_price: nil, product_selections: nil, purchase_price: nil). If that doesn't fix your problem, any second method called within the reduce block on the item variable is able to throw a NoMethodError.

– Sebastian Palma
Nov 26 '18 at 2:09













If I take :purchase_price alone (:product_selections is a joined class, and sale price is a method) this works, and set up a second instance of <% @category_products_count = category.products_by_warehouse_id(params[:id]).where.not(purchase_price: nil).size%> in order to have consistent values for averaging. Submit as an answer?

– Boucherie
Nov 26 '18 at 18:11







If I take :purchase_price alone (:product_selections is a joined class, and sale price is a method) this works, and set up a second instance of <% @category_products_count = category.products_by_warehouse_id(params[:id]).where.not(purchase_price: nil).size%> in order to have consistent values for averaging. Submit as an answer?

– Boucherie
Nov 26 '18 at 18:11














1 Answer
1






active

oldest

votes


















0














So, in order to make sure, that none of the methods being accessed within the reduce block, to the item object are going to return nil, which hence, would throw a NoMethodError, you could firstly check them at the moment of creating your query. A where.not would do that. But keeping in mind that it'll leave aside each record in the database which doesn't satisfy the query criteria.



For that, then:



where.not(
heritable_sale_price: nil,
product_selections: nil,
purchase_price: nil
)


For that, you can analyze the option on setting a default value for each of those columns, so this helps you avoiding the previous query, and having to rescue on each case where there's no value for them. You can see the Rails Migration docs.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473796%2fadding-map-and-or-reject-to-a-reduce-in-rails-view%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    So, in order to make sure, that none of the methods being accessed within the reduce block, to the item object are going to return nil, which hence, would throw a NoMethodError, you could firstly check them at the moment of creating your query. A where.not would do that. But keeping in mind that it'll leave aside each record in the database which doesn't satisfy the query criteria.



    For that, then:



    where.not(
    heritable_sale_price: nil,
    product_selections: nil,
    purchase_price: nil
    )


    For that, you can analyze the option on setting a default value for each of those columns, so this helps you avoiding the previous query, and having to rescue on each case where there's no value for them. You can see the Rails Migration docs.






    share|improve this answer




























      0














      So, in order to make sure, that none of the methods being accessed within the reduce block, to the item object are going to return nil, which hence, would throw a NoMethodError, you could firstly check them at the moment of creating your query. A where.not would do that. But keeping in mind that it'll leave aside each record in the database which doesn't satisfy the query criteria.



      For that, then:



      where.not(
      heritable_sale_price: nil,
      product_selections: nil,
      purchase_price: nil
      )


      For that, you can analyze the option on setting a default value for each of those columns, so this helps you avoiding the previous query, and having to rescue on each case where there's no value for them. You can see the Rails Migration docs.






      share|improve this answer


























        0












        0








        0







        So, in order to make sure, that none of the methods being accessed within the reduce block, to the item object are going to return nil, which hence, would throw a NoMethodError, you could firstly check them at the moment of creating your query. A where.not would do that. But keeping in mind that it'll leave aside each record in the database which doesn't satisfy the query criteria.



        For that, then:



        where.not(
        heritable_sale_price: nil,
        product_selections: nil,
        purchase_price: nil
        )


        For that, you can analyze the option on setting a default value for each of those columns, so this helps you avoiding the previous query, and having to rescue on each case where there's no value for them. You can see the Rails Migration docs.






        share|improve this answer













        So, in order to make sure, that none of the methods being accessed within the reduce block, to the item object are going to return nil, which hence, would throw a NoMethodError, you could firstly check them at the moment of creating your query. A where.not would do that. But keeping in mind that it'll leave aside each record in the database which doesn't satisfy the query criteria.



        For that, then:



        where.not(
        heritable_sale_price: nil,
        product_selections: nil,
        purchase_price: nil
        )


        For that, you can analyze the option on setting a default value for each of those columns, so this helps you avoiding the previous query, and having to rescue on each case where there's no value for them. You can see the Rails Migration docs.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 10:21









        Sebastian PalmaSebastian Palma

        16.1k42135




        16.1k42135
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473796%2fadding-map-and-or-reject-to-a-reduce-in-rails-view%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen