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 6006545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:33:16+00:00 2026-05-23T01:33:16+00:00

I have defined a method called ccy which takes in a number num ,

  • 0

I have defined a method called ccy which takes in a number num, determines the currency (an attribute of the parent Record model) and returns the number multiplied by a conversion factor. Self, in this case, refers to a Setting, which has a number of attributes of its own and belongs to Record. The method is defined in the Setting model below:

class Setting < ActiveRecord::Base
  belongs_to :record

  def ccy(num)
    self.record.currency == "USD" ? ( num * 1 ) :
    self.record.currency == "GBP" ? ( num * 0.616181 ) :
    self.record.currency == "EUR" ? ( num * 0.70618 ) :
    self.record.currency == "CAD" ? ( num * 0.97415 ) : nil
  end
end

This doesn’t work, however, because after doing some tests I’ve discovered that self.record.currency is nil. So, when I try to do something like self.ccy(100) in a rails application, for example, I get the following error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.*

or this, if I’m doing using some sort of operator on the nil element:

TypeError: nil can't be coerced into Fixnum

I’ve looked around online for a bit and I can’t seem to figure out exactly how to fix this. Help appreciated!

  • 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-23T01:33:17+00:00Added an answer on May 23, 2026 at 1:33 am

    In addition to @monocle’s excellent refactoring suggestion:

    You might want to make sure that when you’re saving the Record, there’s a default currency value set.

    Something like:

      validates_presence_of :currency
      before_save :default_currency
      def default_currency
         self.currency = "GBP" unless self.currency.present? #Woo Anglophilia!
      end
    

    You might also have a/n (potentially implicit) :include_blank => true in your currency selector such that you’re getting these empty values stored in the db.

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

Sidebar

Related Questions

I have a class called EventConsumer which defines an event EventConsumed and a method
I have defined [AfterScenario] binding in my stepdefinition class and have method which will
I have a problem using a method defined on a model in CanCan abilities:
Here's my problem: I have a virtual method defined in a .h file that
I want to make a hook method which gets called everytime any function of
I have an iterator with a __len__ method defined. Questions: If you call list(y)
I currently have an object called Station defined as: @interface RFStation : NSObject {
I have a ListActivity containing an object I've defined called a MessageItem. I want
In Ruby, how can I programmatically determine which class/module defines a method being called?
I have defined a class called Stone to add graphical stones to a JPanel:

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.