Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:11:01+00:00 2026-06-11T04:11:01+00:00

I get the error undefined method ‘amount’ for nil:NilClass if the Tax ( tax_id

  • 0

I get the error undefined method 'amount' for nil:NilClass if the Tax (tax_id) is not present on any products.

class Product < ActiveRecord::Base
  attr_accessible :amount, :tax_id
  belongs_to :tax

  def self.total_with_tax
    self.sum(:amount) + all.map(&:tax).map(&:amount).sum
  end
end

class Tax < ActiveRecord::Base
  attr_accessible :amount
  has_many :products
end

Is there a way I can make it so if there’s no Tax ID present when it searches all of the products, it will render it as nil and just do self.sum(:amount)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:11:03+00:00Added an answer on June 11, 2026 at 4:11 am

    Alternatively, you could scope the products to only map those that actually have a tax_id. Something like:

    self.sum(:amount) + self.where("tax_id IS NOT NULL").map(&:tax).map(&:amount).sum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error: undefined method 'vote_sum' for nil:NilClass for this line in my
Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'
This is the error I get undefined method `followed_users?' for #<User:0x007fdadbf11e28> Extracted source (around
When i run my features i get this error: undefined method `visit' for #<Cucumber::Rails::World:0x81b17ac0>
I get the error undefined method `favorite_relationships_path' when I display this form: <%= form_for(current_user.favorite_relationships.build(lesson_id:
I keep getting a undefined method 'comments' for nil:NilClass in my create method when
When I run php-closure i get a PHP error Undefined index: HTTP_IF_NONE_MATCH in <b>/php-closure.php</b>
I am new to Python. I get an error that says undefined Variable. I
In one page of my application I get Type is undefined error on the
I get the following error: 1119: Access of possibly undefined property color through a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.