Skip to content

Instantly share code, notes, and snippets.

@anathematic
Forked from KamaKAzii/method.rb
Last active August 29, 2017 13:51
Show Gist options
  • Save anathematic/3c39a17b37c22a8a0181e8730a7b1bc9 to your computer and use it in GitHub Desktop.
Save anathematic/3c39a17b37c22a8a0181e8730a7b1bc9 to your computer and use it in GitHub Desktop.
def self.purchase_as_guest_with_data(data)
s = create_student_from_data(data)
p = create_parent_from_data(data)
s_user = User.new
s_user.contact = s
s_user.email = s.email
s_user.password = data[:student][:password]
p_user = User.new
p_user.contact = p
p_user.email = p.email
p_user.password = data[:student][:password]
tickets = data[:ticket_allocation_ids].map do |id|
t = Ticket.new
t.ticket_allocation_id = id
t.contact = s
t.purchase_date = Date.today
t
end
begin
ActiveRecord::Base.transaction do
s.save!
p.save!
s_user.save!
p_user.save!
tickets.each { |t| t.save! }
end
rescue ActiveRecord:InvalidRecord # Or something
end
# There's no need for an explict return in Ruby
[s, p, s_user, p_user] + tickets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment