Skip to content

Instantly share code, notes, and snippets.

@dhh
Created May 5, 2014 13:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhh/b0a106a00e8737280666 to your computer and use it in GitHub Desktop.
Save dhh/b0a106a00e8737280666 to your computer and use it in GitHub Desktop.
class Profile < ActiveModel::Aggregator
aggregate :user
aggregate :email
def combine
user.save
user.emails.build(email_attributes).save
end
end
class ProfileController < ApplicationController
def create
@profile = Profile.new(profile_attributes)
if @profile.save
redirect_to somewhere
else
render :new
end
end
end
@dhh
Copy link
Author

dhh commented May 5, 2014

Aggregator could automatically expose its aggregates as prefixed attributes:

<%= form_for @profile do |f| %>
  <%= f.text_field :user_name %>
  <%= f.email_field :email_address %>
  <%= f.password_field :user_password %>
  <%= f.password_field :user_password_confimation %>
<% end%>

@wyaeld
Copy link

wyaeld commented May 17, 2014

why have a separate api for combine, rather than just Profile having save? it is because you don't want to override a save method it would have inherited?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment