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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:23:38+00:00 2026-05-24T09:23:38+00:00

I have a location table I use to store geo coordinates: class Location <

  • 0

I have a location table I use to store geo coordinates:

class Location < ActiveRecord::Base
  # Location has columns/attributes
  #   BigDecimal latitude
  #   BigDecimal longitude

  (...)

  def to_s
    @latitude.to_s << ', ' << @longitude.to_s
  end
end

However, when I call to_s on a Location, the BigDecimal inside converts to an empty string.

ruby > l
 => #<Location id: 1, latitude: #<BigDecimal:b03edcc,'0.4713577E2',12(12)>, longitude: #<BigDecimal:b03ecb4,'-0.7412786E2',12(12)>, created_at: "2011-08-06 03:41:51", updated_at: "2011-08-06 22:21:48"> 
ruby > l.latitude
 => #<BigDecimal:b035fb0,'0.4713577E2',12(12)> 
ruby > l.latitude.to_s
 => "47.13577" 
ruby > l.to_s
 => ", " 

Any idea why?

  • 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-24T09:23:38+00:00Added an answer on May 24, 2026 at 9:23 am

    Your to_s implementation is wrong, it should be this:

    def to_s
        latitude.to_s << ', ' << longitude.to_s
    end
    

    ActiveRecord attributes are not the same as instance variables and accessing @latitude inside the object is not the same as accessing latitude inside the object, @latitude is an instance variable but latitude is a call to a method that ActiveRecord creates for you.

    Also, instance variables auto-create as nil on first use so your original to_s was just doing this:

    nil.to_s << ', ' << nil.to_s
    

    and that isn’t the result you’re looking for.

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

Sidebar

Related Questions

I have a database table named call with columns call_time, location, emergency_type and there
If I have a table with id, location, latitude, longitude and I would have
I have 3 tables event, location, event_location. Event can have multiple locations. Location table
I have table with 50 entries (users with such details like Name Surname Location
I have a database table which link locations together; a location can be in
I have 2 tables product and history product table: id name type price location
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have a table of locations with latitude, longitude and the U.S. state fields.
I have a Location class which represents a location somewhere in a stream. (The
I have an Incident class that uses the HasMany attribute to a Location class.

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.