Initializing a new user
Here's my notes


First don't use #map and use #pluck instead (for database calls), all over your code.

bad:

@membership.user.accounts.map(&amp;:id)


better:

@membership.user.accounts.pluck(:id)

Use eager loading, to reduce queries

Bad:

Membership.find(member_id)


Better:

Membership.includes(user: :accounts).find(member_id)


Because you are using users and accounts in the loops after.
This whole block could be reduced to a single statement

if user_edit_params
  cloned_user.email = invite_email
  cloned_user.permissions = user_edit_params[:permissions]
  cloned_user.accounts = Account.find(invite_app_access.map(&amp;:to_i))
  cloned_user.level = user_edit_params[:user_level]
end


To

if user_edit_params
  cloned_user.assign_attributes(
    email: invite_email
    permissions: user_edit_params[:permissions]
    accounts: Account.find(invite_app_access.map(&amp;:to_i))
    level: user_edit_params[:user_level]
  )
end

No idea what this does

@membership.user.attributes.slice('permissions')


but wouldn't this do the same ?

@membership.user.permissions

Law of Demeter
And basically just group common stuff into single methods and call the methods inside this method, isntead of having one huge method like this