Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make attributes default to 0 if blank or if not validating numericality

I want the attributes of my UserPrice model to default to 0 if they are empty or if its not validating numericality. These attributes are tax_rate, shipping_cost and price.

class CreateUserPrices < ActiveRecord::Migration
  def self.up
    create_table :user_prices do |t|
      t.decimal :price, :precision => 8, :scale => 2
      t.decimal :tax_rate, :precision => 8, :scale => 2
      t.decimal :shipping_cost, :precision => 8, :scale => 2
    end
  end
end

At first, I put :default => 0 inside of the table for all 3 columns but I didn't want that because it already had the fields filled in and I want to use placeholders. Here is my UserPrice model:

class UserPrice < ActiveRecord::Base
  attr_accessible :price, :tax_rate, :shipping_cost
  validates_numericality_of :price, :tax_rate, :shipping_cost
  validates_presence_of :price
end

ANSWER

before_validation :default_to_zero_if_necessary, :on => :create

private 

def default_to_zero_if_necessary
    self.price = 0 if self.price.blank?
    self.tax_rate = 0 if self.tax_rate.blank?
    self.shipping_cost = 0 if self.shipping_cost.blank?
end
like image 975
LearningRoR Avatar asked Jan 17 '12 13:01

LearningRoR


2 Answers

You can probably just place it in a before_validation lifecycle action:

before_validation :default_to_zero_if_necessary

private
  def default_to_zero_if_necessary
    price = 0 if price.blank?
    tax_rate = 0 if tax_rate.blank?
    shipping_cost = 0 if shipping_cost.blank?
  end

You don't need to check if the input is a string since Rails will default it to 0 in that scenario anyways. If you only need this validation during the create action, then you can change it to:

before_validation :default_to_zero_if_necessary, :on => :create
like image 141
iwasrobbed Avatar answered Sep 21 '22 08:09

iwasrobbed


In this case I would probably set :default => 0, :nil => false in the db migrations.

class CreateUserPrices < ActiveRecord::Migration
  def self.up
    create_table :user_prices do |t|
      t.decimal :price, :precision => 8, :scale => 2, :default => 0, :nil => false
      t.decimal :tax_rate, :precision => 8, :scale => 2, :default => 0, :nil => false
      t.decimal :shipping_cost, :precision => 8, :scale => 2, :default => 0, :nil => false
    end
  end
end

I use Attribute Normalizer when having to do more advanced stuff like formatting a phone number https://github.com/mdeering/attribute_normalizer. Attribute normalizer is really good for ensuring format of data.

# here I format a phone number to MSISDN format (004670000000)
normalize_attribute :phone_number do |value|
  PhoneNumberTools.format(value)
end

# use this (can have weird side effects)
normalize_attribute :price do |value|
  value.to_i
end

# or this.
normalize_attribute :price do |value|
  0 if value.blank?
end
like image 38
sunkencity Avatar answered Sep 19 '22 08:09

sunkencity