Skip to content
lyang edited this page Mar 16, 2013 · 2 revisions

CRUD

BraintreeRails provides the same CRUD interface as ActiveRecord objects.

Create

# create in vault
customer = BraintreeRails::Customer.create(:first_name => 'Foo')

# Initialize and save to create
customer = BraintreeRails::Customer.new(:first_name => 'Foo')
customer.save

Retrieve

# Find for given id
customer = BraintreeRails::Customer.find('customer_id')

# Another way to find for given id
customer = BraintreeRails::Customer.new('customer_id')

# Wrap Braintree objects
customer = BraintreeRails::Customer.new(Braintree::Customer.find('customer_id'))

Update

# Assign new value and save
customer = BraintreeRails::Customer.find('customer_id')
customer.company = 'Foo Bar'
customer.save

# Returns true or false when validations failed. Customer#errors will be populated when failed
customer.update_attributes(:website => 'www.example.com')

# Returns true or raises BraintreeRails::RecordInvalid exception
customer.update_attributes!(:email => 'foobar@example.com')

Delete

# Delete the customer from the Vault
customer.destroy 

Note

BraintreeRails can only support the same set of CRUD capabilities as the braintree_ruby. For example,

Plan, AddOn and Discount only support GET, the same in BraintreeRails.

Transaction doesn't support update or destroy, but you can create them and change its status afterward through specific methods.

Clone this wiki locally