Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
User registration form object example
class UserRegistrationForm
include ActiveModel::Model
attr_accessor :name, :email, :phone, :password, :password_confirmation, :min_price, :max_price,
:rating, :discount, :provider_ids, :warranty, :keywords
validates :phone, presence: true
def save
return false unless valid?
save_models
# ...
# additional code related to user creation if necessary, such as sending notifications or log event
true
end
def user
@user ||= User.new(name: name, email: email, phone: phone, password: password,
password_confirmation: password_confirmation, user_request: user_request)
end
private
def save_models
ActiveRecord::Base.transaction do
user_request.save!
user.save!
end
end
def user_request
@user_request ||= UserRequest.new(min_price: min_price, max_price: max_price, rating: rating,
discount: discount, provider_ids: provider_ids,
warranty: warranty, keywords: keywords)
end
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.