Skip to content

Instantly share code, notes, and snippets.

@dhh

dhh/aggregator.rb

Created May 5, 2014
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@dhh 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

This comment has been minimized.

Copy link

@wyaeld 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
You can’t perform that action at this time.