Skip to content

Instantly share code, notes, and snippets.

@kalleth
Forked from onlymejosh/gist:1148780
Created August 16, 2011 10:16
Show Gist options
  • Save kalleth/1148787 to your computer and use it in GitHub Desktop.
Save kalleth/1148787 to your computer and use it in GitHub Desktop.
class Account < ActiveRecord::Base
has_many :users
accepts_nested_attributes_for :users
serialize :account #If you want to store the recurly account, you need to serialise the object
validates_presence_of :company, :on => :create, :message => "can't be blank"
# Before_create will be called once only
before_create :create_external_account
validates :must_have_external_account
def create_external_account
user_for_account = users.first
account = Recurly::Account.new(
:account_code => user_for_account.generate_a_code,
:first_name => user_for_account.first_name,
:last_name => user_for_account.last_name,
:email => 'rachael@test.com',
:company_name => company
)
end
end
class User < ActiveRecord::Base
belongs_to :account
attr_accessible :email, :password, :password_confirmation,
:first_name, :last_name, :company
has_secure_password
validates_presence_of :first_name, :on => :create, :message => "can't be blank"
validates_presence_of :last_name, :on => :create, :message => "can't be blank"
validates_presence_of :email, :on => :create, :message => "can't be blank"
validates_uniqueness_of :email, :on => :create, :message => "has already been taken"
validates_presence_of :password, :on => :create
end
# Accounts Controller
def create
@account = Account.new(params[:account])
# create recurly account
respond_to do |format|
if @account.save
format.html { redirect_to login_path, notice: 'Congratulations! Please use the form belog to access Arctic Fox.' }
else
format.html { render action: "new" }
end
end
end
So where would I run the validation? It only needs to be done on the creation of account not for each user
Can I access user from accounts even before its been saved?
This is the code I need to run. Preferable
account = Recurly::Account.new(
:account_code => 'rachael',
:first_name => 'Rachael',
:last_name => 'Test',
:email => 'rachael@test.com',
:company_name => 'Chicago, Inc'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment