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

  • SEARCH
  • Home
  • 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 7078313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:31:56+00:00 2026-05-28T06:31:56+00:00

I have recently come upon an error when I added the following to my

  • 0

I have recently come upon an error when I added the following to my environment.rb file:

class Array
  def sum
    self.inject{|sum,x| sum + x }
  end
end

The intended method, which allows me to do array.sum, works perfectly, but it has caused a strange error when I try to access objects via a given objects has_many relationship, for example:

class Device < ActiveRecord::Base
  attr_accessible :name, :device_abilities, :abilities

  has_many :device_abilities, :dependent => :destroy
  has_many :abilities, :through => :device_abilities, :dependent => :destroy
end

class Ability < ActiveRecord::Base
  attr_accessible :name, :device_abilities, :devices

  has_many :device_abilities, :dependent => :destroy
  has_many :devices, :through => :device_abilities, :dependent => :destroy
end

class DeviceAbility < ActiveRecord::Base
  attr_accessible :device_id, :ability_id

  belongs_to :device
  belongs_to :ability
end

This works fine without the sum method in environment.rb, so I can do @device.abilities as normal, but when the sum method is added in environment.rb, i get the following error when trying to do @device.abilities:

undefined method `zero?' for nil:NilClass

Can anyone suggest why this is happening, and how I can work around it? I know that i can sum each array manually, but this doesn’t seem very rails-esque.

Thanks!

  • 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-05-28T06:31:56+00:00Added an answer on May 28, 2026 at 6:31 am

    sum is already defined by rails as Enumerable#sum, so you shouldn’t have to define it at all.

    Also, I don’t think environment.rb is the right place to put this sort of thing. You should create a new initializer file if you want to extend base classes. Your new method is probably interfering with rails’ version of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have recently come to this error , i search a lot but i
I have recently come across an interesting question on strings. Suppose you are given
I have recently started learning c# and have come across an issue. I'm not
I have recently been working on a Pastebin script (for fun) and I've come
A few questions have come up recently involving the Application.Evaluate method callable from Excel
I have never come across this issue but most recently I noticed that a
My co-worker and I have come across this warning message a couple times recently.
I have recently been working on implementing an ajax based file uploader for my
I recently came upon the need to have compile-time assertions in C++ to check
Possible Duplicate: What is the “??” operator for? I have recently come across the

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.