0

We use Rails 3.2.18 with activerecord-sqlserver-adapter gem. Everything was fine until we noticed an issue that decimal column is treated as integer. Tuns out that despite the fact that column has decimal(18,2) type set, ActiveRecords sees it as decimal(18,0), and treats attribute as integer.

> Model.columns_hash['decimal_attr'].sql_type
=> "decimal(18,0)"

> Model.columns_hash['decimal_attr'].type
=> :integer

Any suggestions? I'd be happy to get at least a direction to dig this.

Edit: this is an existing table in a legacy database

Alex Ponomarev
  • 885
  • 5
  • 11
  • Mapping from SQL Server should be decimal to decimal - http://stackoverflow.com/questions/4685009/rails-3-datatypes. I suggest have a look at default values http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column and if necessary overwrite. – Pavel Nefyodov Jul 11 '14 at 14:20
  • @PavelNefyodov, unfortunately this is an existing table in legacy database, I cannot recreate it. – Alex Ponomarev Jul 12 '14 at 02:46

1 Answers1

1

Well, in case someone will stumble upon same issue here is a quick hack I came up with. After model has been saved a simple UPDATE query is issued as raw SQL. This does not solve the issue, but allows legacy app that uses the same db to get decimal(18,2) value. Here is a code for a query:

value = ActiveRecord::Base.sanitize(@value)
query = "UPDATE [models] SET [attribute] = #{value} WHERE [models].[id] = #{@model.id}"
Property.connection.execute(query)
Alex Ponomarev
  • 885
  • 5
  • 11