# Datatype error in ROR

**URL:** <https://www.ruby-forum.com/t/datatype-error-in-ror/184704>\
**Category:** Rails\
**Created:** [March 7, 2010, 2:26am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704 "2010-03-07T02:26:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 7, 2010, 2:26am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/1 "2010-03-07T02:26:00Z")

</div>

I have been facing some problem with the datatypes. I have a sample of  
code  
as follows.

t.lat\_rad = (l.lat \* BigDecimal.new(“3.1415926536 / 180”)).to\_f  
t.lng\_rad = (l.lng \* BigDecimal.new(“3.1415926536 / 180”)).to\_f

Initially, the datatype of lat\_rad and lng\_rad was as follows:

t.decimal :lat\_rad, :precision =\> 15, :scale =\> 10  
t.decimal :lng\_rad, :precision =\> 15, :scale =\> 10

So, it gave me an error as follows:

TypeError in ItemsController#populate

wrong argument type BigDecimal (expected Float)

Then I changed the data type of lat\_rad and lng\_rad to float. Even then  
it  
is giving me the same error. Any help to solve this problem will be  
appreciated. Thanks in advance.

–  
View this message in context:  
[http://old.nabble.com/datatype-error-in-ROR-tp27808361p27808361.html](http://old.nabble.com/datatype-error-in-ROR-tp27808361p27808361.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 7, 2010, 2:27am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/2 "2010-03-07T02:27:45Z")

</div>

On Sat, Mar 6, 2010 at 4:39 PM, mamathahl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> t.decimal :lat\_rad, :precision =\> 15, :scale =\> 10  
> appreciated. Thanks in advance.

What’s the type of

l.lat  
l.lng

Also, are the following lines within a migration or controller file?

t.lat\_rad = (l.lat \* BigDecimal.new(“3.1415926536 / 180”)).to\_f  
t.lng\_rad = (l.lng \* BigDecimal.new(“3.1415926536 / 180”)).to\_f

Lastly, what version of Ruby and which database you’re using?

-Conrad

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 7, 2010, 5:05am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/3 "2010-03-07T05:05:13Z")

</div>

Conrad,  
The data type of l.lat and l.lng are same as that of lat\_rad and  
lng\_rad.  
They are declared as follows:

t.decimal :lat, :precision =\> 15, :scale =\> 10  
t.decimal :lng, :precision =\> 15, :scale =\> 10

The following lines are within the controller file named  
items\_controller.rb

t.lat\_rad = (l.lat \* BigDecimal.new(“3.1415926536 / 180”)).to\_f  
t.lng\_rad = (l.lng \* BigDecimal.new(“3.1415926536 / 180”)).to\_f

I’m using Ruby version 4.2 on Ubuntu 8.10.  
The database I’m using is mysql.

Conrad T. wrote:

> > 
> 
> > –  
> > .  
> > removed\_email\_address@domain.invalid.  
> > For more options, visit this group at  
> > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
View this message in context:  
[http://old.nabble.com/datatype-error-in-ROR-tp27808361p27808998.html](http://old.nabble.com/datatype-error-in-ROR-tp27808361p27808998.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 7, 2010, 6:17am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/4 "2010-03-07T06:17:12Z")

</div>

On Sat, Mar 6, 2010 at 8:03 PM, mamathahl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> t.lat\_rad = (l.lat \* BigDecimal.new(“3.1415926536 / 180”)).to\_f  
> t.lng\_rad = (l.lng \* BigDecimal.new(“3.1415926536 / 180”)).to\_f
> 
> I’m using Ruby version 4.2 on Ubuntu 8.10.  
> The database I’m using is mysql.

Ruby version 4.2 doesn’t exist. Please execute the following command  
and post the results:

ruby -v

Next, what’s the exact value of both l.lat and l.lng?

-Conrad

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 7, 2010, 9:43am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/5 "2010-03-07T09:43:13Z")

</div>

I had actually executed the command  
dpkg -s ruby  
It had given me the version as 4.2  
When I did a ruby -v, it gave me the version as 1.8.7

l.lat will have some value such as 40.755970  
l.lng will have some value such as -73.986702

But since they have been declared as Decimal(15,10), their values will  
be  
interpreted as  
40.7559700000 and -73.9867020000

I have a database which consists of latitude and longitude of a few  
cities  
in US. So, this was just one example that I have given.

Conrad T. wrote:

> > 
> 
> Ruby version 4.2 doesn’t exist. Please execute the following command
> 
> > > > 
> > > 
> > > l.lng
> > 
> > removed\_email\_address@domain.invalid.
> > 
> > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).  
> > > You received this message because you are subscribed to the Google G.  
> > > –  
> > > You received this message because you are subscribed to the Google G.  
> > > “Ruby on Rails: Talk” group.  
> > > To post to this group, send email to removed\_email\_address@domain.invalid.  
> > > To unsubscribe from this group, send email to  
> > > removed\_email\_address@domain.invalid.  
> > > For more options, visit this group at  
> > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
View this message in context:  
[http://old.nabble.com/datatype-error-in-ROR-tp27808361p27809867.html](http://old.nabble.com/datatype-error-in-ROR-tp27808361p27809867.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 7, 2010, 10:43am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/6 "2010-03-07T10:43:30Z")

</div>

On 7 March 2010 00:39, mamathahl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> t.decimal :lng\_rad, :precision =\> 15, :scale =\> 10
> 
> So, it gave me an error as follows:
> 
> TypeError in ItemsController#populate
> 
> wrong argument type BigDecimal (expected Float)
> 
> Then I changed the data type of lat\_rad and lng\_rad to float. Even then it  
> is giving me the same error. Any help to solve this problem will be  
> appreciated. Thanks in advance.

I would suggest first splitting the statement into several lines  
performing a single operation on each line, this will enable you to  
identify which part of the statement is generating the error. Also  
try playing in ruby console to try and see what is going on. Finally  
you could use ruby-debug to break in to the code and examine the types  
of the variables to find what may not be quite as you expect.

Also you did not answer the question as to whether this is in a  
migration. If so then it fraught with problems, it is not advisable  
to seed data within a migration if that is what you are doing.

Colin

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 8, 2010, 4:39am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/7 "2010-03-08T04:39:52Z")

</div>

Colin, this is in a migration.  
But I did not understand the last para of your reply. As you have  
suggested,  
I will first try it on script/console and den let me try to know where  
exactly the error is happening.

Colin L.-4 wrote:

> > Initially, the datatype of lat\_rad and lng\_rad was as follows:  
> > Then I changed the data type of lat\_rad and lng\_rad to float. Even then
> 
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid.  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
View this message in context:  
[http://old.nabble.com/datatype-error-in-ROR-tp27808361p27816821.html](http://old.nabble.com/datatype-error-in-ROR-tp27808361p27816821.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 8, 2010, 10:20am UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/8 "2010-03-08T10:20:31Z")

</div>

On 8 March 2010 03:38, mamathahl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Colin, this is in a migration.  
> But I did not understand the last para of your reply.

Have a look at  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/f23f96c71facf726](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/f23f96c71facf726)  
for a discussion on why it is not a good idea to seed in migrations.  
Also google for ‘rails migration seed’ will provide many hits.

Could you not top post please? If you had put the above query under  
the offending paragraph we would not have had to scroll down to find  
to what you were referring. Thanks.

Colin

As you have suggested,

---

<div class="post-metadata">

**Author:** ![mamathahl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mamathahl](https://www.ruby-forum.com/u/mamathahl)\
**Post date:** [March 8, 2010, 7:56pm UTC](https://www.ruby-forum.com/t/datatype-error-in-ror/184704/9 "2010-03-08T19:56:31Z")

</div>

Thanks once again. I’m sorry for giving you the trouble to scroll down  
to  
see the query that I was referring to. I am quite new to nabble.  
henceforth, I’ll make it a point to top post such things

Colin L.-4 wrote:

> > Colin L.-4 wrote:
> > 
> > > > Initially, the datatype of lat\_rad and lng\_rad was as follows:  
> > > > Then I changed the data type of lat\_rad and lng\_rad to float. Even  
> > > > of the variables to find what may not be quite as you expect.  
> > > > “Ruby on Rails: Talk” group.  
> > > > View this message in context:  
> > > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).  
> > > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
View this message in context:  
[http://old.nabble.com/datatype-error-in-ROR-tp27808361p27825847.html](http://old.nabble.com/datatype-error-in-ROR-tp27808361p27825847.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).
