Skip to content

Instantly share code, notes, and snippets.

@dazl
Created December 5, 2010 23:12
Show Gist options
  • Save dazl/729586 to your computer and use it in GitHub Desktop.
Save dazl/729586 to your computer and use it in GitHub Desktop.
Rails zip2tax example
# Rails zip2tax example
# Tested in Rails 3.0.0
# Because of the use of a MySQL stored procedure the CLIENT_MULTI_RESULTS flag
# needs to be set.
# Rails 3 sets that flag and this code works in Rails 3
# Rails <2.3.4 does not and this will not work - see https://gist.github.com/729555
# Rails >= 2.3.4 does -- the code below was only tested with rails 3
# in config/database.yml
zip2tax:
adapter: mysql
host: db.Zip2Tax.com
database: zip2tax
username: z2t_link
password: H^2p6~r
# in the model, app/models/zip2tax.rb
class Zip2tax < ActiveRecord::Base
establish_connection :zip2tax
def self.result_hash(zip_code,username="sample",password="password")
connection.execute("CALL zip2tax.z2t_lookup('#{zip_code}','#{username}','#{password}')").fetch_hash
end
end
# in the controller
@sales_tax_rate = Zip2tax.result_hash('90210','sample','password')["Sales_Tax_Rate"]
# in the view
<%= @sales_tax_rate %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment