Skip to content

Instantly share code, notes, and snippets.

@rvanlieshout
Created March 16, 2011 09:26
Show Gist options
  • Save rvanlieshout/872229 to your computer and use it in GitHub Desktop.
Save rvanlieshout/872229 to your computer and use it in GitHub Desktop.
def create
@user = User.find(params[:responsibility][:user_id])
@company = Company.find(params[:company_id])
# create an array of responsibilities that we want to save
responsibilities = params[:responsibility][:division_ids].collect do |division_id|
Responsibility.new(:division_id => division_id, :user_id => @user.id)
end
# get a list of invalid responsibilities
@invalid_responsibilities = responsibilities.collect do |responsibility|
responsibility unless responsibility.valid?
end.compact
if @invalid_responsibilities.length > 0
# we have one or more invalid responsibilites
render :index and return
else
# all new responsibility are valid so delete the current ones
Responsibility.delete_all(:user_id => @user.id)
# and apply the new ones
responsibilites.each(&:save)
# continue...
flash[:notice] = t('successfully_changed_responsibilities')
redirect_to company_profile_responsibilities_path(@company, @user.profile)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment