Oh my god! where all my digits went?

Case 1: decimals
You are troubled to find out that some of your precision is lost…

1. First place to look is whether you are using the correct number class.

2. next place to look is db column definition
decimal 10,4 for instance, will support 6 digits number with 4 digits after the decimal point.
Don’t get too depressed, you can easily migrate your column to support more digit numbers:


class ExpandDecimals < ActiveRecord::Migration
def self.up
change_column :[table], :[column], :decimal, :precision => 12, :scale => 2
end

def self.down
change_column :[table], :[column], :decimal, :precision => 10, :scale => 4
end
end

Case 2: blob
You are using a table column to store data blob and it’s too short to contain all data?

Migration can be something like:

class ExpandLongtext < ActiveRecord::Migration
def self.up
change_column :[table], :[column], :text, :limit => 16777215
end

def self.down
change_column :[table], :[column], :string, :limit => 255
end
end

 

Migration is very easy and makes your life easy.

Use it.

Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: