Instantly share code, notes, and snippets.

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.

Owner

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.

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