title: Rails - Add Record to join table from controller
body: <p>I'm trying to create a record within a join table from the action of a button. I would have an events model and would like to track selected events from each user.</p>
  
  <p>I used the HABTM relationship since I don't really need any extra fields.</p>
  
  <p>User.rb:</p>
  
  <pre><code>has_to_and_belongs_to_many :events
  </code></pre>
  
  <p>Event.rb:</p>
  
  <pre><code>has_to_and_belongs_to_many :users
  </code></pre>
  
  <p>Events_Users Migration:</p>
  
  <pre><code>[user_id, event_id, id=&gt;false]
  </code></pre>
  
  <p>I'm getting stuck on the actual creation of the record. Someone helped me earlier with adding the record in within the console:</p>
  
  <pre><code>u = User.find(1)
  u.events &lt;&lt; Event.find(1) 
  </code></pre>
  
  <p>Now I would like to perform the action as a result of clicking a link... Is this in the right direction?</p>
  
  <pre><code>def add
    @user = User.find(session[:user_id])
    @event = Event.find(params[:id])
    if @user.events.save(params[:user][:event])
      flash[:notice] = 'Event was saved.'
    end
  end
  </code></pre>
  
  <p>Should I add a <code>@user.events.new</code> somewhere and if so where do I put the params of which user and which event?</p>