title: rails updating a user's record by their email
body: <p>I have an app with an account model and an account manager (user).  </p>
  
  <p>I would like the account manager to be able to add other users to the account by typing in their email.  </p>
  
  <p>The account id of the account manager should be added to the new user's record. </p>
  
  <p>My model looks like </p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
    has_many users
    ...
  end
  
  class User &lt; ActiveRecord::Base
    belongs_to account
    ...
  end
  </code></pre>
  
  <p>In the console it's fairly simple.  </p>
  
  <pre><code>u = User.find(2) 
  u.update_attributes(account_id: 1)
  </code></pre>
  
  <p>But I'm not sure how to do this on the site.  </p>
  
  <p>I have the below partial which renders fine but when I put an email in the box nothing happens.  </p>
  
  <p>I get the message, user not added.</p>
  
  <pre><code>&lt;h4&gt;Add User&lt;/h4&gt;
  &lt;div class = "row"&gt;
      &lt;div class="col-md-6 col-md-offset-3"&gt;
          &lt;%= form_for(:add_user, url: add_users_path) do |f| %&gt;
        &lt;%= f.hidden_field @account = Account.find(params[:id]) %&gt;
        &lt;%= f.label :email %&gt;
        &lt;%= f.email_field :email, class:'form-control' %&gt;
        &lt;%= f.submit "Submit", class: "btn btn-primary" %&gt;
    &lt;% end %&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I don't necessarily want/need to route to another page for example "url: add_users_path" but without that line the code breaks and won't render.</p>
  
  <p>I added this to user.rb</p>
  
  <pre><code>def add_user_to_account
    update_attribute(:account_id)
  end
  </code></pre>
  
  <p>I also created this controller </p>
  
  <pre><code>class AddUsersController &lt; ApplicationController
      before_action :get_user,   only: [:edit, :update]
  
    def new
    end
  
    def edit
    end
  
    def create
      @user = User.find_by(email: params[:email])
      if @user
          @user.add_user_to_account
          flash[:info] =  "Added User to Account"
          redirect_to accounts_url
      else 
          flash[:danger] = "User NOT Added"
          redirect_to accounts_url
      end 
    end
  
    def update
      if params[:user][:account_id].empty?
        @user.errors.add(:account_id, "can't be empty")
        render 'edit'
      elsif @user.update_attributes(user_params)
        log_in @user
        flash[:success] = "Account Updated"
        redirect_to @user
      else
        render 'edit'
      end
    end  
  
  
    private
  
      def user_params
        params.require(:user).permit(:account_id, :user_id)
      end
  
      def get_user
        @user = User.find_by(email: params[:email])
      end
  end
  </code></pre>
  
  <p>Am I on the right track?  Is there a better way to do this?  </p>
  
  <p>UPDATE: Added schema</p>
  
  <pre><code>create_table "accounts", force: :cascade do |t|
      t.string   "name"
      t.integer  "account_manager_id"
      t.integer  "subscription_id"
      t.datetime "created_at",         null: false
      t.datetime "updated_at",         null: false
    end
  
  create_table "users", force: :cascade do |t|
     t.string   "name"
      t.string   "password_digest",   null: false
      t.string   "remember_digest"
      t.string   "email",             null: false
      t.string   "activation_digest"
      t.boolean  "activated"
      t.datetime "activated_at"
      t.string   "reset_digest"
      t.datetime "reset_sent_at"
      t.datetime "created_at",        null: false
      t.datetime "updated_at",        null: false
      t.boolean  "access_granted"
      t.boolean  "requested_access"
      t.string   "slug"
      t.integer  "account_id"
    end
  </code></pre>