Using infinite floats in Ruby on Rails

In a Rails application using SQLite3, I would like to use the values ​​float Float::INFINITY and -Float::INFINITY in a model with a floating point attribute. Performing INSERT Queries Using Model.create! this seems to work fine, since activerecord uses prepared statements in this case. However, when I try to update the record with foo.save , activerecord does not use the prepared stament and just puts the Infinity string directly into the query, resulting in

 SQLite3::SQLException: no such column: Infinity 

Is there a way around this or do I need to resort to using strings in the model / database?

Rails version 3.2.21 , SQLite3 version 1.3.10


Change . Now I changed the column type to string in the database migration and used

 serialize :property, Float 

to tell activerecord to store YAML-serialized floats in the database, which makes Float::INFINITY just fine.

+6
source share
1 answer

This is because Float::INFINITY in rails is defined as 1.0 / 0, because sqlite3 does not know what to do with it. You can use a very large number to determine infinity, for example 9e999, as a replacement for the value that you will save in the database

0
source

Source: https://habr.com/ru/post/988273/


All Articles