Rails : Spree : Zipcode, phonenumber required false

If you need the shipping address to be set any how, and want to skip the validations as set  in the spree’s address model, you can do it easily

module Spree
  class Address < Spree::Base
    require 'twitter_cldr'

    belongs_to :country, class_name: "Spree::Country"
    belongs_to :state, class_name: "Spree::State"

    has_many :shipments, inverse_of: :address

    validates :firstname, :lastname, :address1, :city, :country, presence: true
    validates :zipcode, presence: true, if: :require_zipcode?
    validates :phone, presence: true, if: :require_phone?

    validate :state_validate, :postal_code_validate


# models/spree/address_decorator.rb
Spree::Address.class_eval do
  def require_phone?

  def require_zipcode?


And you are done

Rails : Auto updating geo-coded object attributes

Lets say you are setting your model such that, user only needs to set / input their zipcode and whenever they modify the zipcode the application needs to re-calculate the address information like country, city, state, etc.

# in geo-codable model
geocoded_by :zip_code

after_validation :geocode, if: ->(obj){ obj.zip_code.present? && obj.zip_code_changed? }
after_validation :reverse_geocode, if: ->(obj){ obj.zip_code.present? && obj.zip_code_changed? }

reverse_geocoded_by :latitude, :longitude do |obj, results|
  if geo = results.first
    obj.state = geo.state
    obj.city = geo.city

In the setup above, zip_code field in the table is used to gather the geo-coordinates which will be stored in lat and long field. This is forward geo-coding.

And whenever zip_code changes, we need to recalculate the long, lat . Also we need to re-update the state and city information.

So, the code above does the stuff smoothly.