Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Feedback on http://t.co/sFZYBp08bV - Sorry for being such an advocate for tap :)
def call(form_object)
form_object.validate!
if User.where(email: form_object.email).exists?
form_object.errros.add(:email, 'email already taken')
end
# Forgot to access the company_name attribute on the form_object
company = Company.create!(name: form_object.company_name)
User.create!(name: form_object.name, email: form_object.email, company: company)
form_object
rescue form_object.class::ValidationError
form_object # we have errors here
rescue ActiveRecord::RecordInvalid => e
# add additional errors to form object
end
#-- VS ----------------------------------------------------------------
def call(form_object)
form_object.tap do |this|
this.validate!
if User.where(email: this.email).exists?
this.errros.add(:email, 'email already taken')
end
User.create!({
name: this.name,
email: this.email,
company: Company.create!({
name: this.company_name
})
})
end
rescue form_object.class::ValidationError
form_object # we have errors here
rescue ActiveRecord::RecordInvalid => e
# add additional errors to form object
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.